import { AppType, DeviceType } from '@ucap-webmessenger/core';
import {
  APIRequest,
  APIResponse,
  APIEncoder,
  APIDecoder
} from '@ucap-webmessenger/api';

export interface CompanyListRequest extends APIRequest {
  userSeq?: string;
  appType?: AppType;
  deviceType?: DeviceType;
  token?: string;
  companyGroupCode: string;
}

export interface Company {
  companyCode?: string;
  companyName?: string;
  companyDomain?: string;
  companyConfAuthYn?: string;
  ucapUseYn?: string;
  companyTimerChatAuthYn?: string;
}

export interface CompanyListResponse extends APIResponse {
  companyList?: Company[];
}

export const encodeCompanyList: APIEncoder<CompanyListRequest> = (
  req: CompanyListRequest
) => {
  return {
    p_user_seq: req.userSeq,
    p_app_type: req.appType,
    p_device_type: req.deviceType,
    p_token: req.token,
    p_comp_group_code: req.companyGroupCode
  };
};

export const decodeCompanyList: APIDecoder<CompanyListResponse> = (
  res: any
) => {
  let companyList: Company[] | undefined;
  if (!!res.CompanyList) {
    companyList = [];
    for (const company of res.CompanyList) {
      companyList.push({
        companyCode: company.COMPANY_CODE,
        companyName: company.COMPANY_NAME,
        companyDomain: company.COMPANY_DOMAIN,
        companyConfAuthYn: company.COMPANY_CONF_AUTH,
        ucapUseYn: company.UCAP_USE_YN,
        companyTimerChatAuthYn: company.COMPANY_TIMER_CHAT_AUTH
      });
    }
  }

  return {
    statusCode: res.StatusCode,
    errorMessage: res.ErrorMessage,
    companyList
  } as CompanyListResponse;
};
