function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _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(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _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(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import { IconFont } from '@zgfe/business-lib';
import { Spin, Table, Input, Form } from 'antd';
import React, { useEffect, useState } from 'react';
import _ from 'lodash';
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,
    actName = props.actName,
    setActName = props.setActName;
  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(Form, null, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u52A8\u4F5C\u540D\u79F0",
    colon: false,
    className: "action-name-box ".concat(actName === '' && 'error')
  }, /*#__PURE__*/React.createElement(Input, {
    value: actName,
    maxLength: 50,
    showCount: true,
    onChange: function onChange(e) {
      return setActName(_.trim(e.currentTarget.value));
    }
  }), actName === '' && /*#__PURE__*/React.createElement("div", {
    className: "action-name-box-error"
  }, "\u8BF7\u8F93\u5165\u52A8\u4F5C\u540D\u79F0"))), /*#__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;