/**
 * 
 *     @dfsj/echarts： 专业的水文曲线组件或构造函数以及通用的echart二次封装图表
 *     版本: v3.7.0
 *     作者：yangbo <1747837358@qq.com>
 *     日期：2025-05-27 15:53:33
 *     
 *     
 */

import { read, utils, writeFile } from 'xlsx';

function exportFile(options) {
  return new Promise(function (resolve, reject) {
    try {
      var book = null;
      switch (options.type) {
        case 'json':
          {
            book = utils.book_new();
            var sheet = utils.json_to_sheet(options.data, options.config);
            utils.book_append_sheet(book, sheet, 'Sheet1');
            break;
          }
        case 'excel2json':
          {
            var reader = new FileReader();
            reader.onload = function (e) {
              var data = e.target.result;
              var wb = read(data, {
                type: 'binary'
              });
              var result = [];
              wb.SheetNames.forEach(function (sheetName) {
                result.push({
                  sheetName: sheetName,
                  sheet: utils.sheet_to_json(wb.Sheets[sheetName], {
                    defval: ''
                  })
                });
              });
              return resolve(result);
            };
            return reader.readAsBinaryString(options.file);
          }
        default:
          {
            var titleArr = [];
            if (options.columns) {
              options.columns.map(function (item) {
                titleArr.push(item.label);
              });
            } else {
              options.data.columns.map(function (item) {
                titleArr.push(item.label);
              });
            }
            exportExcel(options.source || options.data.source, options.file, titleArr, options.sheetName, options.columns || options.data.columns);
            return;
          }
      }
      writeFile(book, options.file);
      resolve(false);
    } catch (exception) {
      console.error(exception);
      reject(exception);
    }
  });
}
function exportExcel(json, name, titleArr, sheetName, options) {
  var data = new Array();
  var keyArray = new Array();
  var getLength = function getLength(obj) {
    var count = 0;
    for (var i in obj) {
      if (obj.hasOwnProperty(i)) {
        count++;
      }
    }
    return count;
  };
  var _loop = function _loop() {
    if (json.hasOwnProperty(key1)) {
      var element = json[key1];
      var rowDataArray = new Array();
      options.map(function (item) {
        if (element.hasOwnProperty(item.field)) {
          var element2 = element[item.field];
          rowDataArray.push(element2);
          if (keyArray.length < getLength(element)) {
            keyArray.push(item.field);
          }
        }
      });
      data.push(rowDataArray);
    }
  };
  for (var key1 in json) {
    _loop();
  }
  data.splice(0, 0, keyArray, titleArr);
  var ws = utils.aoa_to_sheet(data);
  var wb = utils.book_new();
  var wsrows = [{
    hidden: true
  }];
  ws['!rows'] = wsrows;
  utils.book_append_sheet(wb, ws, sheetName);
  writeFile(wb, name + '.xlsx');
}

export { exportFile as default, exportExcel };
