function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
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 { Button, Col, Radio, Row, Select, Skeleton, Space, Tooltip } from 'antd';
import _ from 'lodash';
import React, { useContext, useEffect, useState } from 'react';
import apis from '../../../constants/api';
import request from '../../../utils/ajax';
import { classPrefix } from '../types';
import { DatasetListContext } from './idSource';
import { FormItemInputContext } from 'antd/es/form/context';
var IdSourceItem = function IdSourceItem(_ref) {
  var isSingle = _ref.isSingle,
    fieldType = _ref.fieldType,
    defaultValue = _ref.defaultValue,
    length = _ref.length,
    onChange = _ref.onChange,
    onDel = _ref.onDel;
  var prefix = "".concat(classPrefix, "-create");
  var _useContext = useContext(DatasetListContext),
    datasetList = _useContext.datasetList;
  // 数据集
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    dataset = _useState2[0],
    setDataset = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    failed = _useState6[0],
    setFailed = _useState6[1];
  // 字段列表
  var _useState7 = useState([]),
    _useState8 = _slicedToArray(_useState7, 2),
    columnList = _useState8[0],
    setColumnList = _useState8[1];
  // 映射字段
  var _useState9 = useState(),
    _useState0 = _slicedToArray(_useState9, 2),
    mappingField = _useState0[0],
    setMappingField = _useState0[1];
  // 策略id
  var _useState1 = useState(),
    _useState10 = _slicedToArray(_useState1, 2),
    strategyField = _useState10[0],
    setStrategyField = _useState10[1];
  // 策略id顺序
  var _useState11 = useState('asc'),
    _useState12 = _slicedToArray(_useState11, 2),
    mappingSort = _useState12[0],
    setMappingSort = _useState12[1];
  // 唯一标识
  var _useState13 = useState(),
    _useState14 = _slicedToArray(_useState13, 2),
    uniqueIdentifierField = _useState14[0],
    setUniqueIdentifierField = _useState14[1];
  // 是否更新
  var _useState15 = useState(false),
    _useState16 = _slicedToArray(_useState15, 2),
    uniqueIdentifierOpen = _useState16[0],
    setUniqueIdentifierOpen = _useState16[1];
  var _useState17 = useState(true),
    _useState18 = _slicedToArray(_useState17, 2),
    initing = _useState18[0],
    setIniting = _useState18[1];
  var _useContext2 = useContext(FormItemInputContext),
    contextStatus = _useContext2.status;
  useEffect(function () {
    if (!defaultValue) {
      setIniting(false);
      return;
    }
    setDataset(datasetList.find(function (item) {
      return item.id === defaultValue.mappingDataSetId;
    }));
    setMappingSort(defaultValue.mappingSort);
    setMappingField(defaultValue.mappingField);
    setStrategyField(defaultValue.strategyField);
    setUniqueIdentifierField(defaultValue.uniqueIdentifierField);
    setUniqueIdentifierOpen(!!defaultValue.uniqueIdentifierOpen);
    setIniting(false);
  }, []);
  useEffect(function () {
    if (initing) return;
    queryColumnList();
  }, [dataset, initing]);
  useEffect(function () {
    if (initing) return;
    setMappingField(undefined);
    setUniqueIdentifierField(undefined);
  }, [fieldType]);
  useEffect(function () {
    if (initing) return;
    setStrategyField(undefined);
  }, [isSingle]);
  useEffect(function () {
    if (!onChange) return;
    onChange({
      mappingField: mappingField,
      mappingSort: isSingle ? mappingSort : undefined,
      mappingDataSetId: dataset === null || dataset === void 0 ? void 0 : dataset.id,
      strategyField: strategyField,
      uniqueIdentifierField: uniqueIdentifierField,
      uniqueIdentifierOpen: uniqueIdentifierOpen
    });
  }, [dataset, mappingField, strategyField, uniqueIdentifierField, mappingSort, uniqueIdentifierOpen]);
  function onChangeDataset(_v, option) {
    setMappingField(undefined);
    setStrategyField(undefined);
    setUniqueIdentifierField(undefined);
    setUniqueIdentifierOpen(false);
    setDataset(option);
  }
  // 更新标识切换
  function onChangeUniqueUpdate(switched) {
    setUniqueIdentifierOpen(switched);
  }
  function queryColumnList() {
    if (!dataset) return;
    setLoading(true);
    setFailed(false);
    request(apis.queryDatasetColumns, {
      method: 'get',
      params: {
        tableName: dataset === null || dataset === void 0 ? void 0 : dataset.tableName
      }
    }).then(function (res) {
      if (!res) return;
      setColumnList(res.data);
    }).catch(function (err) {
      console.error('查询表结构失败', err);
      setFailed(true);
    }).finally(function () {
      setLoading(false);
    });
  }
  if (initing) {
    return /*#__PURE__*/React.createElement(Row, {
      className: "".concat(prefix, "-source-item")
    }, /*#__PURE__*/React.createElement(Col, {
      span: 24
    }, /*#__PURE__*/React.createElement(Skeleton.Input, {
      active: true
    })));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "form-list-item"
  }, /*#__PURE__*/React.createElement(Row, {
    className: "".concat(prefix, "-source-title"),
    justify: "start"
  }, /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefix, "-source-title-required")
  }, "*"), "\u6620\u5C04\u8868"), /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefix, "-source-title-required")
  }, "*"), "\u6620\u5C04\u5B57\u6BB5"), /*#__PURE__*/React.createElement(Col, {
    span: 12
  }, /*#__PURE__*/React.createElement("span", {
    className: "".concat(prefix, "-source-title-required")
  }, "*"), "\u552F\u4E00\u6807\u8BC6")), /*#__PURE__*/React.createElement(Row, {
    className: "".concat(prefix, "-source-item"),
    justify: "start"
  }, /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, /*#__PURE__*/React.createElement(Select, {
    options: datasetList,
    fieldNames: {
      label: 'name',
      value: 'id'
    },
    showSearch: true,
    filterOption: function filterOption(search, option) {
      return new RegExp(_.escapeRegExp(search), 'i').test((option === null || option === void 0 ? void 0 : option.name) || '');
    },
    className: (dataset === null || dataset === void 0 ? void 0 : dataset.id) ? "".concat(prefix, "-source-item-normal") : '',
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      className: "".concat(prefix, "-icon")
    }),
    value: dataset === null || dataset === void 0 ? void 0 : dataset.id,
    placeholder: "\u8BF7\u9009\u62E9\u6570\u636E\u96C6",
    onChange: onChangeDataset
  }), !(dataset === null || dataset === void 0 ? void 0 : dataset.id) && contextStatus === 'error' ? (/*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-source-item-tip")
  }, "\u6570\u636E\u96C6\u4E0D\u53EF\u4E3A\u7A7A")) : ''), /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, loading ? (/*#__PURE__*/React.createElement(Skeleton.Input, {
    active: true
  })) : (/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, {
    options: columnList.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        disabled: item.webType !== fieldType
      });
    }),
    disabled: !fieldType,
    fieldNames: {
      label: 'name',
      value: 'name'
    },
    value: mappingField === null || mappingField === void 0 ? void 0 : mappingField.name,
    placeholder: "\u8BF7\u9009\u62E9\u5B57\u6BB5",
    className: (mappingField === null || mappingField === void 0 ? void 0 : mappingField.name) ? "".concat(prefix, "-source-item-normal") : '',
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      className: "".concat(prefix, "-icon")
    }),
    showSearch: true,
    filterOption: function filterOption(search, option) {
      return new RegExp(_.escapeRegExp(search), 'i').test((option === null || option === void 0 ? void 0 : option.name) || '');
    },
    onChange: function onChange(_v, option) {
      setMappingField(option);
    }
  }), !(mappingField === null || mappingField === void 0 ? void 0 : mappingField.name) && contextStatus === 'error' ? (/*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-source-item-tip")
  }, "\u5B57\u6BB5\u4E0D\u53EF\u4E3A\u7A7A")) : ''))), /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, loading ? (/*#__PURE__*/React.createElement(Skeleton.Input, {
    active: true
  })) : (/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Select, {
    options: columnList.map(function (item) {
      return _objectSpread(_objectSpread({}, item), {}, {
        disabled: item.webType !== fieldType
      });
    }),
    disabled: !fieldType,
    fieldNames: {
      label: 'name',
      value: 'name'
    },
    value: uniqueIdentifierField === null || uniqueIdentifierField === void 0 ? void 0 : uniqueIdentifierField.name,
    className: (uniqueIdentifierField === null || uniqueIdentifierField === void 0 ? void 0 : uniqueIdentifierField.name) ? "".concat(prefix, "-source-item-normal") : '',
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      className: "".concat(prefix, "-icon")
    }),
    placeholder: "\u8BF7\u9009\u62E9\u552F\u4E00\u6807\u8BC6",
    showSearch: true,
    filterOption: function filterOption(search, option) {
      return new RegExp(_.escapeRegExp(search), 'i').test((option === null || option === void 0 ? void 0 : option.name) || '');
    },
    onChange: function onChange(_v, option) {
      setUniqueIdentifierField(option);
    }
  }), !(uniqueIdentifierField === null || uniqueIdentifierField === void 0 ? void 0 : uniqueIdentifierField.name) && contextStatus === 'error' ? (/*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-source-item-tip")
  }, "\u552F\u4E00\u6807\u8BC6\u4E0D\u53EF\u4E3A\u7A7A")) : ''))), !isSingle ? (/*#__PURE__*/React.createElement(Col, null, !isSingle && (/*#__PURE__*/React.createElement(Button, {
    onClick: onDel,
    type: "link",
    disabled: length === 1,
    className: "".concat(prefix, "-source-del")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "shanchu1"
  }))))) : null), isSingle ? (/*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Row, {
    className: "".concat(prefix, "-source-title"),
    justify: "start"
  }, /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, /*#__PURE__*/React.createElement(Space, null, "\u7B56\u7565ID", /*#__PURE__*/React.createElement(Tooltip, {
    overlayInnerStyle: {
      width: '468px'
    },
    title: "\u300C\u7B56\u7565ID\u300D\u7684\u4F5C\u7528\uFF1A\u8FD9\u4E2A\u51B3\u5B9A\u65B0\u5F15\u5165ID\u7684\u53D6\u503C\u89C4\u5219\uFF0C\u4F8B\u5982\uFF1A\u60A8\u5F15\u5165\u4E86\r\n\u8BA2\u5355\u8868\u4E2D\u7528\u6237\u7684\u624B\u673A\u53F7\uFF0C\u90A3\u4E48\u7528\u6237\u5728\u6BCF\u6B21\u4E0B\u5355\u65F6\u624B\u673A\u53F7\u53EF\u80FD\u4F1A\u51FA\u73B0\u4E0D\r\n\u540C\u7684\u60C5\u51B5\uFF0C\u914D\u7F6E\u597D\u57FA\u51C6ID\u540E\uFF0C\u5C06\u51B3\u5B9A\u7CFB\u7EDF\u5728\u8BA2\u5355\u8868\u4E2D\u83B7\u53D6\u624B\u673A\u53F7\u7684\u5148\r\n\u540E\u987A\u5E8F\u3002",
    placement: "top"
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishiicon"
  }))))), /*#__PURE__*/React.createElement(Row, {
    className: "".concat(prefix, "-source-item"),
    justify: "start"
  }, /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, loading ? (/*#__PURE__*/React.createElement(Skeleton.Input, {
    active: true
  })) : (/*#__PURE__*/React.createElement(Select, {
    options: columnList,
    fieldNames: {
      label: 'name',
      value: 'name'
    },
    className: "".concat(prefix, "-strategy"),
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      className: "".concat(prefix, "-icon")
    }),
    placeholder: "\u8BF7\u9009\u62E9\u7B56\u7565ID",
    showSearch: true,
    filterOption: function filterOption(search, option) {
      return new RegExp(_.escapeRegExp(search), 'i').test((option === null || option === void 0 ? void 0 : option.name) || '');
    },
    value: strategyField === null || strategyField === void 0 ? void 0 : strategyField.name,
    onChange: function onChange(_v, option) {
      setStrategyField(option);
    }
  }))), /*#__PURE__*/React.createElement(Col, {
    span: 6
  }, /*#__PURE__*/React.createElement(Radio.Group, {
    defaultValue: mappingSort,
    onChange: function onChange(e) {
      setMappingSort(e.target.value);
    }
  }, /*#__PURE__*/React.createElement(Space, {
    size: 48
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "asc"
  }, "\u6B63\u5E8F"), /*#__PURE__*/React.createElement(Radio, {
    value: "desc"
  }, "\u5012\u5E8F"))), !isSingle && (/*#__PURE__*/React.createElement(Button, {
    onClick: onDel,
    type: "link",
    disabled: length === 1,
    className: "".concat(prefix, "-source-del")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "shanchu1"
  }))))))) : null);
};
export default IdSourceItem;