function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { IconFont } from '@zgfe/business-lib';
import { Spin, Table } from 'antd';
import React, { useEffect, useState } from 'react';
var classPrefix = 'flow-merge-config';
export var ColumnCell = function ColumnCell(_ref) {
  var column = _ref.column;
  return /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-column")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: {
      string: 'zifuchuan',
      number: 'shuzhi',
      date: 'riqi'
    }[column.webType]
  }), column.name);
};
var CompareTable = function CompareTable(props) {
  var tableList = props.tableList,
    enableSelect = props.enableSelect,
    defaultChecked = props.defaultChecked;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    columns = _useState2[0],
    setColumns = _useState2[1];
  var _useState3 = useState([]),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  var _useState5 = useState(0),
    _useState6 = _slicedToArray(_useState5, 2),
    matchRows = _useState6[0],
    setMatchRows = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    inited = _useState8[0],
    setInited = _useState8[1];
  /**
   * 监听表列表，发生变化时重塑字段列表
   */
  useEffect(function () {
    updateSourceData();
  }, [tableList]);
  function onChange(selectedRowKeys, selectedRows) {
    if (props.onChange) props.onChange(selectedRows.map(function (row) {
      return row.column;
    }));
  }
  function updateSourceData() {
    var nameMap = {};
    var tableMap = {};
    var list = [];
    var columns_ = [{
      title: '字段名称',
      dataIndex: 'name'
    }, {
      title: '匹配结果',
      dataIndex: 'matchCount',
      render: function render(v, record) {
        return "".concat(v, "/").concat(record.tableCount);
      }
    }];
    var count = 0;
    tableList.forEach(function (table) {
      var _table$columns;
      if (!tableMap[table.name]) {
        count++;
        columns_.push({
          title: table.name,
          dataIndex: "tableeee-".concat(table.name),
          render: function render(column, record) {
            return column ? /*#__PURE__*/React.createElement(ColumnCell, {
              column: column
            }) : '-';
          }
        });
        tableMap[table.name] = true;
      }
      (_table$columns = table.columns) === null || _table$columns === void 0 ? void 0 : _table$columns.forEach(function (column) {
        if (!nameMap[column.name]) {
          nameMap[column.name] = {
            name: column.name,
            column: column
          };
          list.push(nameMap[column.name]);
        }
        nameMap[column.name]["tableeee-".concat(table.name)] = column;
      });
    });
    setColumns(columns_);
    var matchRows_ = 0;
    list.forEach(function (record) {
      var map = {};
      for (var key in record) {
        if (!/^tableeee-/.test(key)) continue;
        var type = record[key].type;
        if (!map[type]) map[type] = 0;
        map[type] += 1;
      }
      record.matchCount = Math.max.apply(Math, _toConsumableArray(Object.values(map)));
      if (record.matchCount > 0) matchRows_++;
      record.conflict = Object.keys(map).length > 1;
      record.tableCount = count;
    });
    setMatchRows(matchRows_);
    setDataSource(list);
    onChange([], list.filter(function (row) {
      return defaultChecked === null || defaultChecked === void 0 ? void 0 : defaultChecked.find(function (column) {
        return row.column.name === column.name;
      });
    }));
    setInited(true);
  }
  var rowSelection = {
    type: 'checkbox',
    defaultSelectedRowKeys: defaultChecked === null || defaultChecked === void 0 ? void 0 : defaultChecked.filter(function (item) {
      // 过滤掉不合法的记录
      var row = dataSource.find(function (a) {
        return a.name === item.name;
      });
      if (!row || row.matchCount === 0 || row.conflict) return false;
      return true;
    }).map(function (item) {
      return item.name;
    }),
    getCheckboxProps: function getCheckboxProps(record) {
      return {
        disabled: record.matchCount === 0 || record.conflict
      };
    },
    onChange: onChange
  };
  if (!inited) {
    return /*#__PURE__*/React.createElement("div", {
      className: classPrefix
    }, /*#__PURE__*/React.createElement(Spin, {
      className: "".concat(classPrefix, "-loading")
    }));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u5408\u5E76\u7ED3\u679C", /*#__PURE__*/React.createElement("span", null, "\u5339\u914D\uFF1A", /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-match")
  }, matchRows)), /*#__PURE__*/React.createElement("span", null, "\u4E0D\u5339\u914D\uFF1A", /*#__PURE__*/React.createElement("span", {
    className: "".concat(classPrefix, "-notmatch")
  }, dataSource.length - matchRows))), /*#__PURE__*/React.createElement(Table, {
    key: defaultChecked === null || defaultChecked === void 0 ? void 0 : defaultChecked.length,
    rowKey: "name",
    columns: columns,
    dataSource: dataSource,
    pagination: false,
    rowSelection: enableSelect ? rowSelection : undefined
  }));
};
export default CompareTable;