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, { 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),
    _useState10 = _slicedToArray(_useState9, 2),
    actName = _useState10[0],
    setActName = _useState10[1];
  var _useState11 = useState([]),
    _useState12 = _slicedToArray(_useState11, 2),
    selectColumns = _useState12[0],
    setSelectColumns = _useState12[1];
  var _useState13 = useState(),
    _useState14 = _slicedToArray(_useState13, 2),
    rule = _useState14[0],
    setRule = _useState14[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, {
    wrapperCol: {
      span: 12
    }
  }, /*#__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("div", {
    className: "".concat(classPrefix, "-title")
  }, /*#__PURE__*/React.createElement("span", {
    className: "title"
  }, "\u5F85\u66FF\u6362\u5B57\u6BB5")), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u9009\u62E9\u5B57\u6BB5",
    colon: false,
    labelAlign: "right",
    className: "verify",
    wrapperCol: {
      span: 12
    }
  }, /*#__PURE__*/React.createElement(ColumnSelect, {
    strickType: true,
    options: columnList,
    value: selectColumns,
    onChange: setSelectColumns
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, /*#__PURE__*/React.createElement("span", {
    className: "title"
  }, "\u586B\u5145\u8BBE\u7F6E")), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u89C4\u5219\u8BBE\u7F6E",
    colon: false,
    labelAlign: "right",
    className: "verify",
    wrapperCol: {
      span: 12
    }
  }, /*#__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;