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

export interface UpdateInfoRequest extends APIRequest {
  deviceType: DeviceType;
}

export interface UpdateInfoResponse extends APIResponse {
  appVersion?: string;
  installUrl?: string;
  launcherAppVersion?: string;
  launcherInstallUrl?: string;
}

const updateInfoEncodeMap = {
  deviceType: 'p_device_type',
};

export const encodeUpdateInfo: APIEncoder<UpdateInfoRequest> = (
  req: UpdateInfoRequest
) => {
  return ParameterUtil.encode(updateInfoEncodeMap, req);
};

export const decodeUpdateInfo: APIDecoder<UpdateInfoResponse> = (res: any) => {
  return {
    statusCode: res.StatusCode,
    errorMessage: res.ErrorMessage,
    appVersion: res.AppVer,
    installUrl: res.InstallURL,
    launcherAppVersion: res.LauncherAppVer,
    launcherInstallUrl: res.LauncherInstallURL,
  } as UpdateInfoResponse;
};
