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, { useEffect, useState, useImperativeHandle } from 'react';
import useNode from './../useNode';
import { Form, Input, Select, Tooltip } from 'antd';
import { IconFont } from '@zgfe/business-lib';
import './dataClean.less';
import ColumnSelect from '../../../../../components/columnSelect';
import _ from 'lodash';
import { getFieldInputErrorInfo, getFieldNameError } from '../utils';
var DateToAge = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var node = _ref.node,
    nodeList = _ref.nodeList,
    editData = _ref.editData,
    setSubmit = _ref.setSubmit;
  var classPrefix = 'date-to-age';
  var _useNode = useNode(node),
    columnList = _useNode.columnList;
  // const columnList: any = [
  //   {
  //     alias: null,
  //     comment: null,
  //     hiveType: null,
  //     isFillField: false,
  //     isPrimaryKey: false,
  //     name: 'aid',
  //     type: 'bigint',
  //     webType: 'number',
  //   },
  // ];
  var _useState = useState({}),
    _useState2 = _slicedToArray(_useState, 2),
    selectColumns = _useState2[0],
    setSelectColumns = _useState2[1];
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    retain = _useState4[0],
    setRetain = _useState4[1];
  var _useState5 = useState(''),
    _useState6 = _slicedToArray(_useState5, 2),
    dateFormat = _useState6[0],
    setDateFormat = _useState6[1];
  var _useState7 = useState('age'),
    _useState8 = _slicedToArray(_useState7, 2),
    ageField = _useState8[0],
    setAgeField = _useState8[1];
  var _useState9 = useState('yyyy-MM-dd'),
    _useState10 = _slicedToArray(_useState9, 2),
    selectDateFormat = _useState10[0],
    setSelectDateFormat = _useState10[1];
  var _useState11 = useState(''),
    _useState12 = _slicedToArray(_useState11, 2),
    errorMsg = _useState12[0],
    setErrorMsg = _useState12[1];
  var _useState13 = useState(''),
    _useState14 = _slicedToArray(_useState13, 2),
    alias = _useState14[0],
    setAlias = _useState14[1];
  useEffect(function () {
    setErrorMsg(getFieldInputErrorInfo(ageField) || (getFieldNameError(nodeList, columnList, ageField, editData) ? '字段名称已存在' : ''));
  }, [ageField]);
  // 编辑
  useEffect(function () {
    var _temp = ['yyyyMMdd', 'yyyy-MM-dd', 'yyyy/MM/dd'];
    if (Object.keys(editData).length > 0) {
      setDateFormat(editData === null || editData === void 0 ? void 0 : editData.dateFormat);
      setAgeField((editData === null || editData === void 0 ? void 0 : editData.ageField) || 'age');
      setAlias((editData === null || editData === void 0 ? void 0 : editData.alias) || '');
      setSelectDateFormat(_temp.indexOf(editData === null || editData === void 0 ? void 0 : editData.dateFormat) === -1 ? 'custom' : (editData === null || editData === void 0 ? void 0 : editData.dateFormat) || 'yyyy-MM-dd');
      setRetain(editData === null || editData === void 0 ? void 0 : editData.retain);
      setSelectColumns(editData === null || editData === void 0 ? void 0 : editData.selectColumns);
    }
  }, []);
  useEffect(function () {
    if (Object.keys(selectColumns).length > 0 && (selectDateFormat === 'custom' && dateFormat || selectDateFormat !== 'custom') && ageField && errorMsg === '') {
      setSubmit(true);
    } else {
      setSubmit(false);
    }
  }, [selectColumns, dateFormat, selectDateFormat, ageField, errorMsg]);
  useImperativeHandle(ref, function () {
    return {
      getData: function getData() {
        return {
          selectColumns: selectColumns,
          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,
          dateFormat: selectDateFormat === 'custom' ? dateFormat : selectDateFormat,
          ageField: ageField,
          alias: alias
        };
      }
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(Form, null, /*#__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(Form.Item, {
    label: "\u51FA\u751F\u65E5\u671F\u683C\u5F0F",
    colon: false
  }, /*#__PURE__*/React.createElement(Select, {
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      style: {
        color: '#5F6085'
      }
    }),
    options: [{
      label: 'yyyyMMdd',
      value: 'yyyyMMdd'
    }, {
      label: 'yyyy-MM-dd',
      value: 'yyyy-MM-dd'
    }, {
      label: 'yyyy/MM/dd',
      value: 'yyyy/MM/dd'
    }, {
      label: '自定义',
      value: 'custom'
    }],
    value: selectDateFormat,
    onChange: function onChange(e) {
      setDateFormat('');
      setSelectDateFormat(e);
    }
  })), selectDateFormat === 'custom' && /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u81EA\u5B9A\u4E49\u683C\u5F0F",
    colon: false
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u51FA\u751F\u65E5\u671F\u683C\u5F0F",
    maxLength: 20,
    showCount: true,
    value: dateFormat,
    onChange: function onChange(e) {
      return setDateFormat(_.trim(e.currentTarget.value));
    }
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5E74\u9F84\u5B57\u6BB5\u540D\u79F0",
    colon: false,
    className: "field-box"
  }, /*#__PURE__*/React.createElement("div", {
    id: "tooltip-box"
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u5E74\u9F84\u5B57\u6BB5\u540D\u79F0",
    className: "".concat(errorMsg && 'error-hint'),
    maxLength: 200,
    showCount: true,
    value: ageField,
    onChange: function onChange(e) {
      return setAgeField(_.trim(e.currentTarget.value));
    },
    suffix: /*#__PURE__*/React.createElement(Tooltip, {
      title: "\u201Cy\u201D\u4EE3\u8868\u201C\u5E74\u201D\uFF0C\u201CM\u201D\u4EE3\u8868\u201C\u6708\u201D\uFF0C\u201Cd\u201D\u4EE3\u8868\u201C\u5929\u201D\uFF0C\u4F8B\u5982\u683C\u5F0F\u201Cyyyy-MM-dd\u201D\u4EE3\u8868\u201C2023-01-01\u201D",
      placement: "topRight",
      getPopupContainer: function getPopupContainer() {
        return document.getElementById('tooltip-box');
      }
    }, /*#__PURE__*/React.createElement(IconFont, {
      type: "tishi",
      style: {
        color: ' #9aa1a9'
      }
    }))
  }), errorMsg && /*#__PURE__*/React.createElement("div", {
    className: "error-hint-text"
  }, errorMsg))), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u5E74\u9F84\u5B57\u6BB5\u6CE8\u91CA",
    colon: false,
    className: "not-must"
  }, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u5E74\u9F84\u5B57\u6BB5\u6CE8\u91CA",
    maxLength: 200,
    showCount: true,
    value: alias,
    onChange: function onChange(e) {
      return setAlias(_.trim(e.currentTarget.value));
    }
  }))));
});
export default DateToAge;