/**
 * 非标接口模块
 */

 import { logger } from "mslib"
 import { ApiResponse } from "./TyApiCommon"
 import { CryptoUtils } from "./CryptoUtils"
 import { ParamDecryptFailedError, ParamEmptyError, ParamError } from "./Errors"
 import Contants from "./Contants";
 import { getNonStandardApiConfig } from "./Config";
 
 /**
  * 非标接口请求基类
  */
 export class ApiRequestBase {
     parkId: any;
 }
 
 /**
  * 默认配置
  */
 const defaults = {
     des3: {
         key: 'Keytop:1234567812345678!',
         iv: '20210723'
     }
 }
 
 /**
  * 非标接口配置
  */
 export const config = Object.assign(defaults, getNonStandardApiConfig());
 
 /**
  * 非标接口装饰器
  * 
  * 自动解密，异常捕获，日志记录，发生异常自动返回异常报文等
  * @param description 服务描述
  * @returns
  */
 export function Service(description?: string): MethodDecorator {
     return function (target: Object, propertyKey: string, descriptor: PropertyDescriptor) {
         logger.info(`${Contants.SS_SERVICE_BASE_URL}${process.env["KT_PID"]}.${target.constructor.name}/1.0`, description);
 
         let method = descriptor.value;
 
         descriptor.value = async function (...args) {
             let response;
             let _error;
 
             try {
                 if (!Contants.IS_DEBUG) {
                     if (!args || !args[0]?.data) throw new ParamEmptyError();
 
                     let plaintext;
                     try {
                         plaintext = CryptoUtils.des3DecryCode(args[0].data, config.des3.key, config.des3.iv);
                     } catch (error) {
                         throw new ParamDecryptFailedError();
                     }
 
                     //解密成功后删除密文
                     delete args[0].data;
 
                     Object.assign(args[0], JSON.parse(plaintext));
                 }
 
                 response = await method.apply(this, args);
             } catch (error) {
                 if (error instanceof ParamError) response = ApiResponse.failed(error.message);
                 else if (typeof error === 'string') response = ApiResponse.failed(error);
                 else response = ApiResponse.failed('服务器异常');
 
                 _error = error;
             }
 
             logger[_error ? 'error' : 'info'](`${target.constructor.name},request:${JSON.stringify(args, logInputJSONReplacer)},response:${JSON.stringify(response)}`, _error);
 
             return response;
         }
     }
 }
 
 /**
  * input 日志记录 JSON replacer
  * 
  * 过滤前端传过来的base64图片字符串
  * @param key 
  * @param value 
  * @returns 
  */
 function logInputJSONReplacer(key, value) {
     if (typeof value === 'string' && value.startsWith('data:')) return 'base64Str';
 
     return value;
 }