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 { Table } from 'antd';
import React, { useEffect, useState } from 'react';
import apis from '../../../../../constants/api';
import request from '../../../../../utils/ajax';
import { ColumnCell } from '../compareTable';
import { classPrefix } from './row2Column';
import Request from 'umi-request';
var Preview = function Preview(_ref) {
  var targetColumn = _ref.targetColumn,
    fillingColumn = _ref.fillingColumn,
    addList = _ref.addList,
    originColumnList = _ref.originColumnList,
    defaultValue = _ref.defaultValue,
    onChange = _ref.onChange;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    list = _useState2[0],
    setList = _useState2[1];
  var _useState3 = useState(Request.CancelToken.source()),
    _useState4 = _slicedToArray(_useState3, 2),
    setCancelToken = _useState4[1];
  var _useState5 = useState(true),
    _useState6 = _slicedToArray(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var _useState7 = useState(0),
    _useState8 = _slicedToArray(_useState7, 2),
    setTimer = _useState8[1];
  var _useState9 = useState([]),
    _useState0 = _slicedToArray(_useState9, 2),
    selectList = _useState0[0],
    setSelectList = _useState0[1];
  var _useState1 = useState([]),
    _useState10 = _slicedToArray(_useState1, 2),
    selectKeys = _useState10[0],
    setSelectKeys = _useState10[1];
  useEffect(function () {
    setSelectList(defaultValue || []);
    setSelectKeys((defaultValue === null || defaultValue === void 0 ? void 0 : defaultValue.map(function (item) {
      return item.name;
    })) || []);
  }, []);
  useEffect(function () {
    setCancelToken(function (_ref2) {
      var cancel = _ref2.cancel;
      cancel();
      var newToken = Request.CancelToken.source();
      queryData(newToken.token);
      return newToken;
    });
  }, [targetColumn, fillingColumn, addList, originColumnList]);
  useEffect(function () {
    if (onChange) onChange(selectList);
  }, [selectList]);
  function queryData(token) {
    setTimer(function (t) {
      clearTimeout(t);
      return setTimeout(function () {
        setLoading(true);
        setList([]);
        request(apis.getR2CPreview, {
          method: 'post',
          data: {
            targetColumn: targetColumn,
            fillingColumn: fillingColumn,
            addList: addList,
            originColumnList: originColumnList
          },
          notifyType: 'warning',
          errorTitle: '友情提示',
          cancelToken: token
        }).then(function (res) {
          if (!res) return;
          setList(res.data);
        }).catch(function (err) {
          console.error('获取预览结果失败', err);
        }).finally(function () {
          setLoading(false);
        });
      }, 300);
    });
  }
  function _onChange(selectedRowKeys, selectedRows) {
    setSelectList(selectedRows);
    setSelectKeys(selectedRowKeys);
  }
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, /*#__PURE__*/React.createElement("span", {
    className: "title"
  }, "\u7ED3\u679C\u9884\u89C8")), /*#__PURE__*/React.createElement(Table, {
    columns: [{
      title: '字段名称',
      dataIndex: 'name',
      render: function render(v, row) {
        return /*#__PURE__*/React.createElement(ColumnCell, {
          column: row
        });
      }
    }],
    rowKey: "name",
    loading: loading,
    rowSelection: {
      type: 'checkbox',
      selectedRowKeys: selectKeys,
      onChange: _onChange
    },
    dataSource: list,
    pagination: false
  }));
};
export default Preview;