function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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 { 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(),
    _useState10 = _slicedToArray(_useState9, 2),
    mappingField = _useState10[0],
    setMappingField = _useState10[1];
  // 策略id
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    strategyField = _useState12[0],
    setStrategyField = _useState12[1];
  // 策略id顺序
  var _useState13 = useState('asc'),
    _useState14 = _slicedToArray(_useState13, 2),
    mappingSort = _useState14[0],
    setMappingSort = _useState14[1];
  // 唯一标识
  var _useState15 = useState(),
    _useState16 = _slicedToArray(_useState15, 2),
    uniqueIdentifierField = _useState16[0],
    setUniqueIdentifierField = _useState16[1];
  // 是否更新
  var _useState17 = useState(false),
    _useState18 = _slicedToArray(_useState17, 2),
    uniqueIdentifierOpen = _useState18[0],
    setUniqueIdentifierOpen = _useState18[1];
  var _useState19 = useState(true),
    _useState20 = _slicedToArray(_useState19, 2),
    initing = _useState20[0],
    setIniting = _useState20[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\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\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\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;