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 React, { useEffect, useState } from 'react';
import useNode from './useNode';
import { DatePicker, Form, Input, Select, Space } from 'antd';
import { IconFont } from '@zgfe/business-lib';
import '../styles/replaceNull.less';
import ConfigLoading from './configLoading';
import CdpFlowTypes from '../../../../types/flow';
import ColumnSelect from '../../../../components/columnSelect';
import moment from 'moment';
import _ from 'lodash';
var Rule = function Rule(_ref) {
  var value = _ref.value,
    webType = _ref.webType,
    onChange = _ref.onChange;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    rule = _useState2[0],
    setRule = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    customValue = _useState4[0],
    setCustomValue = _useState4[1];
  var _useState5 = useState([{
      name: '0',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.zero,
      disabled: false
    }, {
      name: '最小值',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.min,
      disabled: false
    }, {
      name: '最大值',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.max,
      disabled: false
    }, {
      name: '中位数',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.median,
      disabled: false
    }, {
      name: '平均值',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.avg,
      disabled: false
    }, {
      name: '众数',
      value: CdpFlowTypes.NodeData.ReplaceNullRule.mode,
      disabled: false
    }, {
      name: '自定义值',
      value: 'custom',
      disabled: false
    }]),
    _useState6 = _slicedToArray(_useState5, 2),
    options = _useState6[0],
    setOptions = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    inited = _useState8[0],
    setInited = _useState8[1];
  useEffect(function () {
    if (!webType || !value) {
      setInited(true);
      return;
    }
    if (options.find(function (item) {
      return item.value === value;
    })) {
      setRule(value);
    } else {
      if (webType) setRule('custom');
      setCustomValue(value);
    }
    setInited(true);
  }, []);
  useEffect(function () {
    if (!inited) return;
    // 数值类型所有选项可用，非数值类型只有自定义可用
    var isNumber = webType === 'number';
    setOptions(function (prev) {
      return prev.map(function (item) {
        return _objectSpread(_objectSpread({}, item), {}, {
          disabled: !isNumber && item.value !== 'custom'
        });
      });
    });
    // 类型发生改变则重置表单数据
    setRule(undefined);
    setCustomValue(undefined);
  }, [webType]);
  useEffect(function () {
    if (!inited) return;
    if (rule === 'custom') {
      onChange === null || onChange === void 0 ? void 0 : onChange(customValue);
    } else {
      onChange === null || onChange === void 0 ? void 0 : onChange(rule);
    }
  }, [rule, customValue]);
  return /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(Select, {
    options: options,
    suffixIcon: /*#__PURE__*/React.createElement(IconFont, {
      type: "xiangxia",
      style: {
        color: '#5F6085'
      }
    }),
    fieldNames: {
      label: 'name',
      value: 'value'
    },
    style: {
      width: 234
    },
    value: rule,
    onChange: function onChange(v) {
      setRule(v);
    }
  }), rule === 'custom' && (webType === 'date' ? (/*#__PURE__*/React.createElement(DatePicker, {
    value: customValue ? moment(customValue) : undefined,
    onChange: function onChange(date) {
      setCustomValue(date === null || date === void 0 ? void 0 : date.format('YYYY-MM-DD'));
    }
  })) : (/*#__PURE__*/React.createElement(Input, {
    style: {
      width: 234
    },
    type: webType || 'text',
    value: customValue,
    onChange: function onChange(e) {
      return setCustomValue(e.target.value);
    }
  }))));
};
// 替换缺失值动作
var ReplaceNullConfig = function ReplaceNullConfig(_ref2) {
  var node = _ref2.node;
  var classPrefix = 'flow-replacenull-config';
  var _useNode = useNode(node),
    columnList = _useNode.columnList,
    loading = _useNode.loading;
  var _useState9 = useState(node.getData().label),
    _useState0 = _slicedToArray(_useState9, 2),
    actName = _useState0[0],
    setActName = _useState0[1];
  var _useState1 = useState([]),
    _useState10 = _slicedToArray(_useState1, 2),
    selectColumns = _useState10[0],
    setSelectColumns = _useState10[1];
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    rule = _useState12[0],
    setRule = _useState12[1];
  useEffect(function () {
    var data = node.getData();
    if (data) {
      setSelectColumns(data.columns);
      setRule(data.rule);
    }
  }, []);
  useEffect(function () {
    node.updateData({
      status: rule && (selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.length) && actName ? 'success' : 'failed',
      rule: rule,
      label: actName,
      columns: selectColumns
    });
  }, [rule, selectColumns, actName]);
  if (loading) {
    return /*#__PURE__*/React.createElement(ConfigLoading, null);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement(Form, null, /*#__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: "\u5F85\u66FF\u6362\u5B57\u6BB5",
    colon: false,
    labelAlign: "right",
    className: "verify"
  }, /*#__PURE__*/React.createElement(ColumnSelect, {
    strickType: true,
    options: columnList,
    value: selectColumns,
    onChange: setSelectColumns
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u586B\u5145\u89C4\u5219\u8BBE\u7F6E",
    colon: false,
    labelAlign: "right",
    className: "verify"
  }, /*#__PURE__*/React.createElement(Rule, {
    webType: (selectColumns === null || selectColumns === void 0 ? void 0 : selectColumns.length) ? selectColumns[0].webType : undefined,
    value: rule,
    onChange: setRule
  })), /*#__PURE__*/React.createElement(Space, {
    className: "".concat(classPrefix, "-tip")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishi"
  }), "\u5982\u679C\u9009\u62E9\u5B57\u6BB5\u7C7B\u578B\u4E3A\u6570\u503C\uFF0C\u5219\u586B\u5145\u89C4\u5219\u53EF\u90090\u3001\u6700\u5C0F\u503C\u3001\u6700\u5927\u503C\u3001\u4E2D\u4F4D\u6570\u3001\u5E73\u5747\u503C\u3001\u4F17\u6570\u3001\u81EA\u5B9A\u4E49\u503C\u3002\u5B57\u7B26\u7C7B\u578B\u652F\u6301\u81EA\u5B9A\u4E49\u503C\uFF1B\u5176\u4ED6\u7C7B\u578B\u652F\u6301\u81EA\u5B9A\u4E49\u503C\u586B\u5145\uFF0C\u5982\u201C\u5176\u4ED6\u201D\u3002"));
};
export default ReplaceNullConfig;