"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _grid = _interopRequireDefault(require("@alifd/next/lib/grid"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../../_utils/conditionRenderService/type");
var _formKeyType = require("../../_utils/formKeyType");
var _locale = require("../../../src/locale");
require("./index.less");
var _conditionRenderService = require("../../_utils/conditionRenderService");
var _renderByFormKey = _interopRequireDefault(require("./renderByFormKey"));
var _utils = require("../../_utils/utils");
var _lodash = require("lodash");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var Row = _grid["default"].Row,
  Col = _grid["default"].Col;
var conditionRenderService = new _conditionRenderService.ConditionRenderService();
var dataForm = (0, _formKeyType.getConditionRenderList)();
var ConditionRender = function ConditionRender(props) {
  var conditions = props.data,
    isDictionary = props.isDictionary;
  var _useState = (0, _react.useState)([]),
    selectOption = _useState[0],
    setSelectOption = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    selectRowOption = _useState2[0],
    setSelectRowOption = _useState2[1];
  (0, _react.useEffect)(function () {
    conditionRenderService.load(props.data, props.type);
  }, []);
  (0, _react.useEffect)(function () {
    conditionRenderService.load(props.data, props.type);
  }, [conditions]);
  (0, _react.useEffect)(function () {
    var selectOption = conditionRenderService.getSelectRelateByType(props.selectRelate);
    var selectRowOption = conditionRenderService.getSelectRelateByType(props.selectRowRelate);
    setSelectOption(selectOption);
    setSelectRowOption(selectRowOption);
  }, [props.selectRelate, props.selectRowRelate]);
  function onChangeData(obj) {
    var key = obj.key,
      value = obj.value;
    props.onChange({
      key: key,
      value: value
    });
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "ConditionRenderBox"
  }, conditions.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_Condition, (0, _extends2["default"])({}, item, {
      isDictionary: isDictionary,
      conditions: conditions,
      key: item.id,
      selectOption: selectOption,
      selectRowOption: selectRowOption,
      onChange: function onChange(value) {
        onChangeData({
          key: "data",
          value: [].concat(value)
        });
      }
    }));
  }));
};
var _Condition = function Condition(props) {
  var isDictionary = props.isDictionary,
    conditions = props.conditions,
    selectOption = props.selectOption,
    selectRowOption = props.selectRowOption,
    relate = props.relate,
    children = props.children,
    onChange = props.onChange;
  var comData = {
    relate: props.relate,
    rowData: props.rowData,
    id: props.id,
    children: props.children
  };
  function onOutDataData(key, v) {
    var newProps = (0, _lodash.cloneDeep)(comData);
    newProps[key] = v;
    var newConditions = conditionRenderService.alterRowData(newProps);
    props.onChange(newConditions);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "conditionItem"
  }, children.length > 0 && /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-group"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-relation-wrap"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-group-line"
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-relation"
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    value: relate,
    size: "small",
    onChange: function onChange(v) {
      onOutDataData("relate", v);
    },
    dataSource: selectOption
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-list-wrap"
  }, children.map(function (item) {
    return /*#__PURE__*/_react["default"].createElement(_Condition, (0, _extends2["default"])({
      key: item.id
    }, item, {
      isDictionary: true,
      selectOption: selectOption,
      selectRowOption: selectRowOption,
      onChange: onChange
    }));
  }))), children.length === 0 && /*#__PURE__*/_react["default"].createElement(ListOneItem, (0, _extends2["default"])({}, props, {
    conditions: true,
    selectRowOption: selectRowOption
  })));
};
var ListOneItem = function ListOneItem(props) {
  var conditions = props.conditions,
    rowData = props.rowData,
    selectRowOption = props.selectRowOption,
    onChange = props.onChange,
    isDictionary = props.isDictionary;
  var _useState3 = (0, _react.useState)(dataForm),
    conditionList = _useState3[0],
    setConditionList = _useState3[1];
  (0, _react.useEffect)(function () {
    if (isDictionary) {
      var dd = (0, _formKeyType.getDictionaryList)();
      setConditionList([].concat(conditionList, dd));
    }
  }, [isDictionary]);
  var comData = {
    relate: props.relate,
    rowData: props.rowData,
    id: props.id,
    children: props.children
  };
  function copyRowData() {
    var newConditions = conditionRenderService.copyRowData(comData);
    onChange(newConditions);
  }
  function addRowData() {
    var newConditions = conditionRenderService.addRowData(comData);
    onChange(newConditions);
  }
  function removeRowData() {
    var newConditions = conditionRenderService.removeRowData(comData);
    onChange(newConditions);
  }
  function onFormKeyChange(obj, key, v) {
    obj[key] = v;
    var newProps = (0, _lodash.cloneDeep)(comData);
    newProps.rowData = obj;
    var newConditions = conditionRenderService.alterRowData(newProps);
    onChange(newConditions);
  }
  function onRenderByFormKeyChange(list) {
    var newProps = (0, _lodash.cloneDeep)(comData);
    list.forEach(function (item) {
      var key = item.key,
        value = item.value;
      newProps.rowData[key] = value;
    });
    var newConditions = conditionRenderService.alterRowData(newProps);
    onChange(newConditions);
  }
  function onRenderByFormKeyChanges(list, key) {
    var changeObj = comData.rowData[key];
    list.forEach(function (item) {
      changeObj[item.key] = item.value;
    });
    var newProps = (0, _lodash.cloneDeep)(comData);
    newProps.rowData[key] = (0, _extends2["default"])({}, changeObj);
    var newConditions = conditionRenderService.alterRowData(newProps);
    onChange(newConditions);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "conditionListOneItem"
  }, /*#__PURE__*/_react["default"].createElement(Row, {
    className: "conditionListOneItemRow",
    justify: "space-between"
  }, /*#__PURE__*/_react["default"].createElement(Col, {
    span: rowData.relate !== _type.computedRelateType.omit ? "10" : "21",
    className: "conditionListOneItemCol"
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: conditionList,
    value: {
      id: (0, _utils.uuid)(),
      formKey: rowData.leftTree.formKey || _formKeyType.DataSourceType.defaultType,
      data: rowData.leftTree.data,
      treeData: rowData.leftTree.treeData || []
    },
    onChange: function onChange(data) {
      onRenderByFormKeyChanges(data, "leftTree");
    }
  })), /*#__PURE__*/_react["default"].createElement(Col, {
    span: "2",
    className: "conditionListOneItemColRelate"
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    autoWidth: false,
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    dataSource: selectRowOption,
    value: rowData.relate,
    onChange: function onChange(v) {
      onFormKeyChange(rowData, "relate", v);
    }
  })), rowData.relate !== _type.computedRelateType.omit && /*#__PURE__*/_react["default"].createElement(Col, {
    span: "10",
    className: "conditionListOneItemCol"
  }, /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: conditionList,
    value: {
      id: (0, _utils.uuid)(),
      formKey: rowData.rightTree.formKey || _formKeyType.DataSourceType.defaultType,
      data: rowData.rightTree.data,
      treeData: rowData.rightTree.treeData || []
    },
    onChange: function onChange(data) {
      onRenderByFormKeyChanges(data, "rightTree");
    }
  }))), /*#__PURE__*/_react["default"].createElement("div", {
    className: "rowBtn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    disabled: conditionRenderService.isLastData(conditions),
    type: "primary",
    size: "small",
    onClick: function onClick() {
      copyRowData();
    }
  }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
    className: "rowBtnIcon",
    type: "copy"
  })), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    type: "primary",
    size: "small",
    onClick: function onClick() {
      addRowData();
    }
  }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
    type: "add"
  })), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    disabled: conditionRenderService.isLastData(conditions),
    type: "primary",
    size: "small",
    onClick: function onClick() {
      removeRowData();
    }
  }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
    type: "ashbin"
  }))));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(ConditionRender, this.props);
  };
  return _default;
}(_react.Component);