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

export interface TranslationReqRequest extends APIRequest {
  userSeq: number;
  deviceType: DeviceType;
  token: string;
  original: string;
  srcLocale: string;
  destLocale: string;
}

export interface TranslationReqResponse extends APIResponse {
  srcLocale?: string;
  destLocale?: string;
  original?: string;
  translation?: string;
  returnJson?: any;
}

const translationReqEncodeMap = {
  userSeq: 'p_user_seq',
  deviceType: 'p_device_type',
  token: 'p_token',
  original: 'p_original',
  srcLocale: 'p_src_locale',
  destLocale: 'p_dest_locale'
};

export const encodeTranslationReq: APIEncoder<TranslationReqRequest> = (
  req: TranslationReqRequest
) => {
  return ParameterUtil.encode(translationReqEncodeMap, req);
};

export const decodeTranslationReq: APIDecoder<TranslationReqResponse> = (
  res: any
) => {
  try {
    return {
      statusCode: res.StatusCode,
      srcLocale: res.SrcLocale,
      destLocale: res.DestLocale,
      original: res.Original,
      translation: res.Translation,
      returnJson: JSON.stringify(res)
    } as TranslationReqResponse;
  } catch (e) {
    return {
      statusCode: StatusCode.Fail,
      errorMessage: e
    } as TranslationReqResponse;
  }
  return {} as TranslationReqResponse;
};
