"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 _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _react = _interopRequireWildcard(require("react"));
var _type = require("../../../../_utils/conditionRenderService/type");
var _conditionRender = _interopRequireDefault(require("../../../_components/conditionRender"));
var _conditionRenderService = require("../../../../_utils/conditionRenderService");
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; }
var ConditionDialogRender = function ConditionDialogRender(props) {
  var detailsBtnText = props.detailsBtnText,
    resetBtnText = props.resetBtnText,
    value = props.value,
    onChange = props.onChange,
    conditionType = props.conditionType,
    selectRelate = props.selectRelate,
    selectRowRelate = props.selectRowRelate;
  var conditionRenderService = new _conditionRenderService.ConditionRenderService();
  var _useState = (0, _react.useState)({
      visible: false,
      title: "" + (0, _locale.t)("setter.conditionSet") + (0, _locale.t)("setter.details"),
      detailConfig: {
        data: [],
        resultData: {
          isHandle: false,
          resultRowList: []
        }
      }
    }),
    dialogInfo = _useState[0],
    setDialogInfo = _useState[1];
  (0, _react.useEffect)(function () {
    if (value && value.data) {
      conditionRenderService.load(value.data);
    }
  }, []);
  function showConditionPage() {
    var result = {
      visible: true,
      title: "" + (detailsBtnText ? detailsBtnText : (0, _locale.t)("setter.conditionSet")) + (0, _locale.t)("setter.details"),
      detailConfig: value
    };
    setDialogInfo(result);
  }
  function resetRelateList() {
    _dialog["default"].confirm({
      v2: true,
      title: (0, _locale.t)("setter.displayTitle"),
      content: (0, _locale.t)("setter.displayContent"),
      onOk: function onOk() {
        var _data = conditionRenderService.createInitItemOneByType(_type.ConditionType.all);
        setDialogInfo((0, _extends2["default"])({}, dialogInfo, {
          detailConfig: {
            data: [_data],
            resultData: conditionRenderService.createInitResultData()
          }
        }));
      }
    });
  }
  function onConditionRenderChange(obj) {
    var key = obj.key,
      value = obj.value;
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.detailConfig[key] = value;
    setDialogInfo(newDialog);
  }
  function closeDialog() {
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.visible = false;
    setDialogInfo(newDialog);
  }
  function onDetailOk() {
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.visible = false;
    onChange(newDialog.detailConfig);
    setDialogInfo(newDialog);
  }

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

  /**
   * 导入关系配置信息
   */
  function importComputed(data) {
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.detailConfig.data = data;
    setDialogInfo(newDialog);
  }
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    style: {
      marginRight: 10
    },
    type: "primary",
    onClick: showConditionPage
  }, detailsBtnText || (0, _locale.t)("setter.conditionSet")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: function onClick() {
      resetRelateList();
    }
  }, resetBtnText || (0, _locale.t)("setter.displayReset"))), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "logicDetailBox",
    title: /*#__PURE__*/_react["default"].createElement(_copyTitle["default"], {
      title: dialogInfo.title,
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    height: 600,
    visible: dialogInfo.visible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: onDetailOk,
      type: "primary"
    }, (0, _locale.t)('setter.ok')), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        marginLeft: '10px'
      },
      onClick: closeDialog
    }, (0, _locale.t)('setter.cancel'))]
  }, /*#__PURE__*/_react["default"].createElement(_conditionRender["default"], {
    type: conditionType || _type.ConditionType.all,
    selectRelate: selectRelate || _type.SelectRelateType.addAndOr,
    selectRowRelate: selectRowRelate || _type.SelectRelateType.all,
    data: dialogInfo.detailConfig.data,
    onChange: onConditionRenderChange
  })));
};
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(ConditionDialogRender, this.props);
  };
  return _default;
}(_react.Component);