import type { RcFile} from 'antd/es/upload/interface';
import cryptoJS  from 'crypto-js';
import { encode as base64_encode } from 'base-64';
import { decode as base64_decode } from 'base-64';


//密码加密
export const encodePw = (password: string): string => {
  return base64_encode(`${password}`);
};
//密码解秘
export const decodePw = (password: string): string => {
  return base64_decode(`${password}`);
};

export const encryptDES = (data:any) => {
  const key = 'DFOWjx72';
  const keyHex = cryptoJS.enc.Utf8.parse(key);
  const encrypted = cryptoJS.DES.encrypt(data, keyHex,{
    mode: cryptoJS.mode.ECB,
    padding: cryptoJS.pad.Pkcs7
  });
  return cryptoJS.enc.Base64.stringify(encrypted.ciphertext);

};



//预览图片
export const getBase64 = (img: RcFile, callback: (url: string) => void) => {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result as string));
  reader.readAsDataURL(img);
};
//逗号分隔
export function fixTheList(x: any) {
  let list = '';
  if (x === undefined) {
    return '--';
  } else {
    for (let i = 0; i <= x.length - 1; i++) {
      list += x[i].name + ',';
    }
    return list;
  }
}
// search Data format
export const toTrim = (obj: {
  [key: string]: any;
}, data:{
  [key: string]: any;
}) => {
  if (obj) {
    Object.keys(obj).forEach((key) => {
      if (obj[key] || obj[key] === 0) {
        if (typeof (obj[key]) === 'string') {
          data[key] = obj[key].trim();
        } else if (typeof (obj[key]) === 'object') {
          if (obj[key].length > 0) {
            data[key] = obj[key];
          }
        } else {
          data[key] = obj[key];
        }
      }
    });
    return data;
  }
  return {};
};