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 { BizSelect, IconFont } from '@zgfe/business-lib';
import { Skeleton, DatePicker, Input } from 'antd';
import _ from 'lodash';
import moment from 'moment';
import React, { useEffect, useMemo, useState } from 'react';
var classPrefix = 'cdp-column-rules';
var stringOperate = [{
  name: '是',
  value: 'equal'
}, {
  name: '包含',
  value: 'contains'
}, {
  name: '不是',
  value: 'not equal'
}, {
  name: '不包含',
  value: 'not contains'
}, {
  name: '开头是',
  value: 'begin with'
}, {
  name: '结尾是',
  value: 'end with'
}, {
  name: '开头不是',
  value: 'not begin with'
}, {
  name: '结尾不是',
  value: 'not end with'
}, {
  name: '是空值',
  value: 'is null'
}, {
  name: '不是空值',
  value: 'is not null'
}];
var numberOperate = [{
  name: '>',
  value: 'gt'
}, {
  name: '=',
  value: 'equal'
}, {
  name: '<',
  value: 'lt'
}, {
  name: '>=',
  value: 'ge'
}, {
  name: '<=',
  value: 'le'
}, {
  name: '≠',
  value: 'not equal'
}];
var dateOperate = [{
  name: '最近',
  value: 'relative'
}, {
  name: '固定时段',
  value: 'absolute'
}];
var ColumnRule = function ColumnRule(props) {
  var columnList = props.columnList;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    column = _useState2[0],
    setColumn = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    operator = _useState4[0],
    setOperator = _useState4[1];
  var _useState5 = useState([]),
    _useState6 = _slicedToArray(_useState5, 2),
    value = _useState6[0],
    setValue = _useState6[1];
  var _useState7 = useState(true),
    _useState8 = _slicedToArray(_useState7, 2),
    initing = _useState8[0],
    setIniting = _useState8[1];
  /**
   * 初始化表单
   */
  useEffect(function () {
    if (!props.value) {
      setIniting(false);
      return;
    }
    columnList.forEach(function (item) {
      var _props$value;
      if (((_props$value = props.value) === null || _props$value === void 0 ? void 0 : _props$value.column) === item.name) {
        setColumn(item);
        // 初始化操作符
        var list = getOperatorList(item.webType);
        var ope;
        list.forEach(function (o) {
          var _props$value2;
          if (o.value === ((_props$value2 = props.value) === null || _props$value2 === void 0 ? void 0 : _props$value2.operator)) {
            ope = o;
          }
        });
        setOperator(ope);
        // 初始化条件值
        if ((item === null || item === void 0 ? void 0 : item.webType) === 'date') {
          if (ope.value === 'relative') {
            setValue(props.value.value);
          } else {
            setValue(props.value.value);
          }
        } else {
          var _props$value3;
          setValue((_props$value3 = props.value) === null || _props$value3 === void 0 ? void 0 : _props$value3.value);
        }
      }
    });
    setTimeout(function () {
      setIniting(false);
    });
  }, []);
  useMemo(function () {
    if (!column || initing) return;
    var list = getOperatorList(column.webType);
    setOperator(list.length ? list[0] : undefined);
    setValue([]);
  }, [column]);
  useEffect(function () {
    if (initing) return;
    if (['absolute', 'relative', 'is null', 'is not null'].includes(operator === null || operator === void 0 ? void 0 : operator.value)) setValue([]);
  }, [operator]);
  useEffect(function () {
    if (props.onChange) props.onChange({
      column: column === null || column === void 0 ? void 0 : column.name,
      operator: operator === null || operator === void 0 ? void 0 : operator.value,
      value: value
    });
  }, [column, operator, value]);
  function getOperatorList(type) {
    var list = [];
    switch (type) {
      case 'date':
        list = dateOperate;
        break;
      case 'number':
        list = numberOperate;
        break;
      case 'string':
        list = stringOperate;
        break;
      default:
        list = [];
    }
    return list;
  }
  if (initing) {
    return /*#__PURE__*/React.createElement("div", {
      className: "".concat(classPrefix, "-item")
    }, /*#__PURE__*/React.createElement(Skeleton.Input, null));
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-item")
  }, /*#__PURE__*/React.createElement(BizSelect, {
    className: "".concat(classPrefix, "-column"),
    options: columnList || [],
    keyField: "name",
    labelField: "name",
    value: column,
    placeholder: "\u5B57\u6BB5\u9009\u62E9",
    overlayWidth: 160,
    onChange: function onChange(data) {
      setColumn(data);
    }
  }), function () {
    var list = getOperatorList(column === null || column === void 0 ? void 0 : column.webType);
    return /*#__PURE__*/React.createElement(BizSelect, {
      className: "".concat(classPrefix, "-operator"),
      options: list,
      keyField: "value",
      labelField: "name",
      value: operator || list[0],
      onChange: function onChange(data) {
        setValue([]);
        setOperator(data);
      }
    });
  }(), function () {
    switch (column === null || column === void 0 ? void 0 : column.webType) {
      case 'date':
        if ((operator === null || operator === void 0 ? void 0 : operator.value) === 'absolute') return /*#__PURE__*/React.createElement(DatePicker.RangePicker, {
          className: "".concat(classPrefix, "-value date"),
          defaultValue: function () {
            if (_.isArray(value)) return value.map(function (d) {
              return moment(d);
            });
            return undefined;
          }(),
          onChange: function onChange(range) {
            setValue(range === null || range === void 0 ? void 0 : range.map(function (item) {
              return item === null || item === void 0 ? void 0 : item.toDate();
            }));
          }
        });
        if ((operator === null || operator === void 0 ? void 0 : operator.value) === 'relative') return /*#__PURE__*/React.createElement(Input, {
          key: "relative-date",
          className: "".concat(classPrefix, "-value date"),
          type: "number",
          defaultValue: value[0],
          onChange: function onChange(e) {
            e.persist();
            setValue([e.target.value]);
          },
          suffix: "\u5929"
        });
      case 'string':
        if (['is null', 'is not null'].includes(operator === null || operator === void 0 ? void 0 : operator.value)) return null;
        return /*#__PURE__*/React.createElement(Input, {
          className: "".concat(classPrefix, "-value"),
          value: value[0],
          onChange: function onChange(e) {
            e.persist();
            setValue([e.target.value]);
          }
        });
      case 'number':
        return /*#__PURE__*/React.createElement(Input, {
          type: "number",
          className: "".concat(classPrefix, "-value"),
          value: value[0],
          onChange: function onChange(e) {
            e.persist();
            setValue([e.target.value]);
          }
        });
    }
  }(), /*#__PURE__*/React.createElement(IconFont, {
    type: "shanchu1",
    size: 14,
    className: "".concat(classPrefix, "-del"),
    onClick: props.onRemove
  }));
};
export default ColumnRule;