"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 _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _react = _interopRequireWildcard(require("react"));
var _conditionRender = _interopRequireDefault(require("../_components/conditionRender"));
var _type = require("../../_utils/conditionRenderService/type");
var _conditionRenderService = require("../../_utils/conditionRenderService");
var _lodash = require("lodash");
var _copyTitle = _interopRequireDefault(require("../_components/copyTitle"));
var _locale = require("../../../src/locale");
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; }
/**
 * 不展示条件后期也可以使用事件流逐渐替换
 * 后期可和cComputed-setter 整合成一个
 */

var conditionRenderService = new _conditionRenderService.ConditionRenderService();
var CNoDisplayConditionSetter = function CNoDisplayConditionSetter(props) {
  var type = _type.ConditionType.display;
  var selectRelate = _type.SelectRelateType.addAndOr;
  var selectRowRelate = _type.SelectRelateType.compare;
  var _useState = (0, _react.useState)(false),
    dialogVisible = _useState[0],
    setDialogVisible = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    conditions = _useState2[0],
    setConditions = _useState2[1];
  (0, _react.useEffect)(function () {
    initData();
  }, []);
  function initData() {
    var result = [];
    if ((0, _lodash.isObject)(props.value) && props.value.type !== undefined && props.value.data !== undefined) {
      result = props.value.data;
    } else {
      var data = conditionRenderService.createInitItemOneByType(type);
      result = [data];
    }
    setConditions(result);
  }
  function comOnChange(conditions) {
    var data = {
      type: type,
      selectRelate: selectRelate,
      selectRowRelate: selectRowRelate,
      data: conditions
    };
    props.onChange(data);
  }
  function handleOk() {
    setDialogVisible(false);
    comOnChange(conditions);
  }
  function closeDialog() {
    setDialogVisible(false);
  }
  function resetCondition() {
    _dialog["default"].confirm({
      v2: true,
      title: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, (0, _locale.t)("setter.displayTitle")),
      content: /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, (0, _locale.t)("setter.displayContent")),
      onOk: function onOk() {
        var data = conditionRenderService.createInitItemOneByType(_type.ConditionType.computed);
        comOnChange([data]);
        setConditions([data]);
      }
    });
  }

  /**
   * 导出关系配置信息
   */
  function exportComputed() {
    return conditionRenderService.getCopyConditionData(conditions);
  }

  /**
   * 导入关系配置信息
   */
  function importComputed(data) {
    setConditions(data);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "CComputedBox"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    style: {
      marginRight: 10
    },
    type: "primary",
    onClick: function onClick() {
      setDialogVisible(true);
    }
  }, (0, _locale.t)("setter.displayDisposition")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: function onClick() {
      resetCondition();
    }
  }, (0, _locale.t)("setter.displayReset")), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "CComputed-dialog",
    title: /*#__PURE__*/_react["default"].createElement(_copyTitle["default"], {
      title: (0, _locale.t)("setter.displayConfig"),
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    visible: dialogVisible,
    onClose: closeDialog
    // eslint-disable-next-line react/jsx-no-bind
    ,
    onCancel: closeDialog,
    onOk: function onOk() {
      return handleOk();
    }
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    type: type,
    selectRelate: selectRelate,
    selectRowRelate: selectRowRelate,
    data: conditions,
    onChange: function onChange(obj) {
      var key = obj.key,
        value = obj.value;
      if (key === "data") {
        setConditions(value);
      }
    }
  })));
};
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(CNoDisplayConditionSetter, this.props);
  };
  return _default;
}(_react.Component);