import baseX from 'base-x';
import { isEmpty, template } from 'lodash';
import { apiRequest } from '../utils/request';
var BASE16 = '0123456789abcdef';
var bs16 = baseX(BASE16);

export function Base16Encode(value) {
  let is_open_base14 = sessionStorage.getItem('is_open_base16')
    ? JSON.parse(sessionStorage.getItem('is_open_base16'))
    : false;
  if (is_open_base14) return value ? bs16.encode(new Buffer(value)) : null;
  return value;
}

const giveKey = (key, datas) => {
  if (datas && datas[key]) {
    datas[key] = Base16Encode(datas[key]);
  }
  return datas;
};


const encryptApiList = [
  {
    api: 'DataColumnApi/getOptions',
    key: ['filterSql'],
  },
  {
    api: 'DataColumnApi/getLabels',
    key: ['allValues'],
    query: true,
    setNull: true,
  },
  {
    api: 'DataColumnApi/getSqlOptions',
    key: ['allValues'],
    query: true,
  },
  {
    api: 'DataColumnApi/getSqlLabels',
    key: ['allValues'],
    query: true,
  },
  {
    api: 'DataColumnApi/getSqlData',
    key: ['allValues', 'sqlKey'],
    query: true,
  },
  {
    api: 'DataRightApi/add',
    key: ['filterSql'],
  },
  {
    api: 'DataRightApi/addBatch',
    key: ['filterSql'],
  },
  {
    api: 'DataObjApi/add',
    key: ['sqlScript'],
  },
  {
    api: 'DataObjApi/getFormDataList',
    key: ['custom'],
  },
  {
    api: 'DataObjApi/getFormDataPage',
    key: ['sql', 'custom'],
  },
  {
    api: 'UserApi/getPage',
    key: ['addSignSql'],
  },
  {
    api: 'UnifiedAppFormApi/examineProcess',
    key: ['taskForm'],
  },
  {
    api: 'UnifiedAppFormApi/saveDraft',
    key: ['content'],
  },
  {
    api: 'UnifiedAppFormApi/startProcess',
    key: ['content'],
  },
  {
    api: 'UnifiedAppFormApi/startProcessByService',
    key: ['content'],
  },
  {
    api: 'UnifiedAppFormApi/getHandleUser',
    key: ['content'],
  },
]
export async function giveBase16EnCode(datas, url) { // 全局加解密函数.
  if (url && typeof url === 'string') {
    for (let item of encryptApiList) {
      if (url.indexOf(item.api) > -1) {
        for (let g of item.key) {
          if (g == "allValues") {
            if (item.setNull) {
              datas.allValues = "{}";
            } else {
              if (item.query) {
                if (datas.sqlKey.length < 13) {
                  const response = await apiRequest("/SqlManageEntityApi/findParamsKey", { sqlKey: datas.sqlKey });

                  if (response.length == 0) {
                    datas.allValues = "{}";
                  } else {
                    const x = JSON.parse(allValues);
                    const y = {};
                    for (var i in response) {
                      if (x[i]) {
                        y[i] = x[i]
                      }
                      datas.allValues = JSON.stringify(y);
                    }
                  }
                } else {
                  datas.allValues = "{}";
                }
              }
            }
            datas = giveKey(g, datas);
          }
        }
        return datas;
      }
    }
  }

  return datas;
}
