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 React, { Fragment, useEffect, useState } from 'react';
import useNode from './useNode';
import { Form, Checkbox, Input, Select, Radio, InputNumber, Table } from 'antd';
import { util } from '@zgfe/business-lib';
import '../styles/splitColumn.less';
import ConfigLoading from './configLoading';
import ColumnSelect from '../../../../components/columnSelect';
import { getFieldInputErrorInfo, getFieldNameError } from './utils';
import _ from 'lodash';
// 拆分列
var SplitColumnConfig = function SplitColumnConfig(_ref) {
  var node = _ref.node,
    nodeList = _ref.nodeList;
  var classPrefix = 'split-columns-config';
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    form = _Form$useForm2[0];
  var nodeData = node.getData();
  var _useState = useState(nodeData.label),
    _useState2 = _slicedToArray(_useState, 2),
    actName = _useState2[0],
    setActName = _useState2[1];
  var _useNode = useNode(node),
    columnList = _useNode.columnList,
    loading = _useNode.loading;
  var initialValuesHandle = function initialValuesHandle(formValue) {
    var _data = {};
    // 拆分方式
    _data.splitType = (formValue === null || formValue === void 0 ? void 0 : formValue.splitType) ? formValue === null || formValue === void 0 ? void 0 : formValue.splitType : 'separator';
    if (_data.splitType === 'separator') {
      _data.rule = (formValue === null || formValue === void 0 ? void 0 : formValue.rule) ? formValue === null || formValue === void 0 ? void 0 : formValue.rule : 'any';
    }
    return _objectSpread(_objectSpread({}, formValue), _data);
  };
  var initTableData = {
    name: '',
    alias: '',
    type: 'string'
  };
  var getInitTableData = function getInitTableData(num) {
    var _arr = [];
    for (var index = 0; index < num; index++) {
      _arr.push(_objectSpread(_objectSpread({}, initTableData), {}, {
        key: util.guid()
      }));
    }
    return _arr;
  };
  var _useState3 = useState(nodeData.splitResult || getInitTableData(2)),
    _useState4 = _slicedToArray(_useState3, 2),
    dataSource = _useState4[0],
    setDataSource = _useState4[1];
  var _useState5 = useState(nodeData.splitResult || getInitTableData(2)),
    _useState6 = _slicedToArray(_useState5, 2),
    dataSourceTemp = _useState6[0],
    setDataSourceTemp = _useState6[1];
  var _useState7 = useState(nodeData.columns || {}),
    _useState8 = _slicedToArray(_useState7, 2),
    selectColumns = _useState8[0],
    setSelectColumns = _useState8[1];
  var _useState9 = useState(_objectSpread(_objectSpread({}, initialValuesHandle(nodeData)), {}, {
      separator: (nodeData === null || nodeData === void 0 ? void 0 : nodeData.customSeparator) ? 'custom' : nodeData === null || nodeData === void 0 ? void 0 : nodeData.separator
    })),
    _useState10 = _slicedToArray(_useState9, 2),
    formValue = _useState10[0],
    setFormValue = _useState10[1];
  var _useState11 = useState(nodeData === null || nodeData === void 0 ? void 0 : nodeData.retain),
    _useState12 = _slicedToArray(_useState11, 2),
    retain = _useState12[0],
    setRetain = _useState12[1];
  var statusHandle = function statusHandle() {
    if (!actName || Object.keys(selectColumns).length <= 0) {
      return false;
    }
    var errorList = [];
    dataSource.map(function (item) {
      if (item.name === '' || item.nameError !== '') errorList.push(false);
    });
    if (formValue.splitType === 'separator') {
      // 按分隔符拆分
      // 未填写自定义分隔符
      if (formValue.separator === 'custom' && !formValue.customSeparator) errorList.push(false);
      if (formValue.rule === 'any') {
        // 拆分规则： 任意分隔符
        if (!formValue.splitCount) errorList.push(false);
      } else {
        // 拆分规则： 指定分隔符
        if (!formValue.splitPosition) errorList.push(false);
      }
    } else {
      // 按位置拆分
      if (!formValue.splittingPosition) errorList.push(false);
    }
    if (errorList.length <= 0) {
      return true;
    } else {
      return false;
    }
  };
  useEffect(function () {
    node.updateData(_objectSpread(_objectSpread({}, formValue), {}, {
      label: actName,
      optField: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.name,
      optFieldType: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.type,
      webType: selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.webType,
      retain: retain,
      columns: selectColumns,
      status: statusHandle() ? 'success' : 'failed',
      splitResult: dataSource,
      rule: formValue === null || formValue === void 0 ? void 0 : formValue.rule,
      splitCount: formValue === null || formValue === void 0 ? void 0 : formValue.splitCount,
      splitType: formValue === null || formValue === void 0 ? void 0 : formValue.splitType,
      customSeparator: (formValue === null || formValue === void 0 ? void 0 : formValue.separator) === 'custom' ? formValue.customSeparator : '',
      separator: (formValue === null || formValue === void 0 ? void 0 : formValue.separator) === 'custom' ? formValue.customSeparator : formValue.separator
    }));
  }, [selectColumns, formValue, retain, dataSource, actName]);
  var onValuesChange = function onValuesChange(data, allData) {
    var _allData, _allData2;
    if (((_allData = allData) === null || _allData === void 0 ? void 0 : _allData.splitType) === 'separator') {
      // 按分隔符拆分
      delete allData.splittingPosition;
      allData = initialValuesHandle(allData);
    } else {
      // 按位置拆分
      delete allData.separator;
      delete allData.customSeparator;
      delete allData.rule;
      delete allData.splitCount;
      delete allData.splitPosition;
    }
    if (((_allData2 = allData) === null || _allData2 === void 0 ? void 0 : _allData2.rule) === 'any') {
      delete allData.splitPosition;
    } else {
      delete allData.splitCount;
    }
    setFormValue(_objectSpread({}, allData));
    if ((data === null || data === void 0 ? void 0 : data.splitType) || (data === null || data === void 0 ? void 0 : data.rule)) {
      form.resetFields();
      form.setFieldsValue(_objectSpread({}, allData));
    }
    if ((data === null || data === void 0 ? void 0 : data.rule) || (data === null || data === void 0 ? void 0 : data.splitType) === 'position') {
      setDataSource(getInitTableData(2));
    }
    if (data === null || data === void 0 ? void 0 : data.splitCount) {
      setDataSource(getInitTableData((data === null || data === void 0 ? void 0 : data.splitCount) || 2));
    }
    if (data === null || data === void 0 ? void 0 : data.splittingPosition) {
      setDataSource(getInitTableData(2));
    }
  };
  var fieldChange = function fieldChange(value, text, key) {
    var _dataSource = [];
    dataSource.map(function (item) {
      if (item.key === key) {
        item[text] = value;
      }
      _dataSource.push(item);
    });
    setDataSourceTemp(_dataSource);
  };
  useEffect(function () {
    // 错误信息判断逻辑处理
    var _list = getFieldNameError(nodeList, columnList, '', nodeData, dataSourceTemp, 'repeatList');
    dataSourceTemp.map(function (item) {
      var _temp_name = getFieldInputErrorInfo(item.name);
      if (_list.indexOf(item.name) !== -1 || _temp_name) {
        if (_list.indexOf(item.name) !== -1) item.nameError = '字段名称已存在';
        if (_temp_name) item.nameError = _temp_name;
      } else {
        item.nameError = '';
      }
    });
    setDataSource(dataSourceTemp);
  }, [dataSourceTemp]);
  var tableColumns = [{
    title: /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("span", {
      className: "requiredIcon"
    }, "*"), "\u7ED3\u679C\u5B57\u6BB5\u540D\u79F0"),
    dataIndex: 'name',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        className: "".concat(record.nameError && 'error-hint'),
        placeholder: "\u8BF7\u8F93\u5165",
        value: text,
        onChange: function onChange(e) {
          return fieldChange(_.trim(e.currentTarget.value), 'name', record.key);
        },
        maxLength: 200,
        showCount: true
      }), record.nameError && /*#__PURE__*/React.createElement("div", {
        className: "error-hint-text"
      }, record.nameError));
    }
  }, {
    title: '注释',
    dataIndex: 'alias',
    render: function render(text, record) {
      return /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
        placeholder: "\u8BF7\u8F93\u5165",
        value: text,
        onChange: function onChange(e) {
          return fieldChange(_.trim(e.currentTarget.value), 'alias', record.key);
        },
        maxLength: 200,
        showCount: true
      }));
    }
  }];
  if (loading) {
    return /*#__PURE__*/React.createElement(ConfigLoading, null);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(Form, {
    form: form,
    onValuesChange: onValuesChange,
    initialValues: _objectSpread(_objectSpread({}, formValue), {}, {
      separator: (formValue === null || formValue === void 0 ? void 0 : formValue.separator) ? formValue === null || formValue === void 0 ? void 0 : formValue.separator : ';'
    })
  }, /*#__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(Form.Item, {
    label: "\u9009\u62E9\u5B57\u6BB5",
    colon: false
  }, /*#__PURE__*/React.createElement(ColumnSelect, {
    options: columnList.filter(function (item) {
      return ['number', 'string'].includes(item.webType);
    }),
    value: selectColumns,
    onChange: setSelectColumns,
    multiple: false
  }), /*#__PURE__*/React.createElement(Checkbox, {
    checked: retain,
    onChange: function onChange(e) {
      return setRetain(e.target.checked);
    }
  }, "\u4FDD\u7559\u539F\u5B57\u6BB5")), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u62C6\u5206\u65B9\u5F0F",
    name: "splitType",
    colon: false,
    rules: [{
      required: true,
      message: '请选择拆分方式'
    }]
  }, /*#__PURE__*/React.createElement(Radio.Group, null, /*#__PURE__*/React.createElement(Radio, {
    value: "separator"
  }, "\u6309\u5206\u9694\u7B26\u62C6\u5206"), /*#__PURE__*/React.createElement(Radio, {
    value: "position"
  }, "\u6309\u4F4D\u7F6E\u62C6\u5206"))), formValue.splitType === 'position' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u62C6\u5206\u4F4D\u7F6E",
    name: "splittingPosition",
    colon: false,
    rules: [{
      required: true,
      message: '请输入拆分位置'
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    precision: 0,
    placeholder: "\u8BF7\u8F93\u5165\u62C6\u5206\u4F4D\u7F6E",
    min: 1,
    max: 10
  })), formValue.splitType === 'separator' && /*#__PURE__*/React.createElement(Fragment, null, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5206\u9694\u7B26",
    name: "separator",
    colon: false,
    rules: [{
      required: true,
      message: '请选择分隔符'
    }]
  }, /*#__PURE__*/React.createElement(Select, {
    options: [{
      label: ';',
      value: ';'
    }, {
      label: ',',
      value: ','
    }, {
      label: '/',
      value: '/'
    }, {
      label: '-',
      value: '-'
    }, {
      label: '自定义',
      value: 'custom'
    }]
  })), formValue.separator === 'custom' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u81EA\u5B9A\u4E49\u5206\u9694\u7B26",
    name: "customSeparator",
    colon: false,
    rules: [{
      required: true,
      message: '请输入自定义分隔符'
    }],
    getValueFromEvent: function getValueFromEvent(event) {
      return event.target.value.replace(/\s+/g, '');
    }
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u81EA\u5B9A\u4E49\u5206\u9694\u7B26",
    maxLength: 5,
    showCount: true
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5206\u9694\u7B26\u89C4\u5219",
    name: "rule",
    colon: false,
    rules: [{
      required: true,
      message: '请选择分隔符规则'
    }]
  }, /*#__PURE__*/React.createElement(Radio.Group, null, /*#__PURE__*/React.createElement(Radio, {
    value: "any"
  }, "\u4EFB\u610F\u4F4D\u7F6E\u7684\u5206\u9694\u7B26"), /*#__PURE__*/React.createElement(Radio, {
    value: "position"
  }, "\u6307\u5B9A\u4F4D\u7F6E\u7684\u5206\u9694\u7B26"))), formValue.rule === 'any' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u62C6\u5206\u5217\u6570",
    name: "splitCount",
    colon: false,
    rules: [{
      required: true,
      message: '请输入拆分列数'
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    precision: 0,
    placeholder: "\u8BF7\u8F93\u5165\u62C6\u5206\u5217\u6570",
    min: 1,
    max: 10
  })), formValue.rule === 'position' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5206\u9694\u7B26\u4F4D\u7F6E",
    name: "splitPosition",
    colon: false,
    rules: [{
      required: true,
      message: '请输入分隔符位置'
    }]
  }, /*#__PURE__*/React.createElement(InputNumber, {
    precision: 0,
    placeholder: "\u8BF7\u8F93\u5165\u5206\u9694\u7B26\u4F4D\u7F6E",
    min: 1,
    max: 10
  })))), /*#__PURE__*/React.createElement(Table, {
    className: "".concat(classPrefix, "-table"),
    columns: tableColumns,
    dataSource: dataSource,
    // loading={loading}
    pagination: false,
    rowKey: "key"
  }));
};
export default SplitColumnConfig;