import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Button from "@alifd/next/es/button";
import _extends from "@babel/runtime/helpers/extends";
import _Dialog from "@alifd/next/es/dialog";
import React, { Component, useEffect, useState } from "react";
import { ConditionType, SelectRelateType } from "../../../../_utils/conditionRenderService/type";
import ConditionRender from "../../../_components/conditionRender";
import { ConditionRenderService } from "../../../../_utils/conditionRenderService";
import CopyTitle from "../../../_components/copyTitle";
import { t } from "../../../../../src/locale";
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();
  var _useState = useState({
      visible: false,
      title: "" + t("setter.conditionSet") + t("setter.details"),
      detailConfig: {
        data: [],
        resultData: {
          isHandle: false,
          resultRowList: []
        }
      }
    }),
    dialogInfo = _useState[0],
    setDialogInfo = _useState[1];
  useEffect(function () {
    if (value && value.data) {
      conditionRenderService.load(value.data);
    }
  }, []);
  function showConditionPage() {
    var result = {
      visible: true,
      title: "" + (detailsBtnText ? detailsBtnText : t("setter.conditionSet")) + t("setter.details"),
      detailConfig: value
    };
    setDialogInfo(result);
  }
  function resetRelateList() {
    _Dialog.confirm({
      v2: true,
      title: t("setter.displayTitle"),
      content: t("setter.displayContent"),
      onOk: function onOk() {
        var _data = conditionRenderService.createInitItemOneByType(ConditionType.all);
        setDialogInfo(_extends({}, 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.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(_Button, {
    style: {
      marginRight: 10
    },
    type: "primary",
    onClick: showConditionPage
  }, detailsBtnText || t("setter.conditionSet")), /*#__PURE__*/React.createElement(_Button, {
    onClick: function onClick() {
      resetRelateList();
    }
  }, resetBtnText || t("setter.displayReset"))), /*#__PURE__*/React.createElement(_Dialog, {
    className: "logicDetailBox",
    title: /*#__PURE__*/React.createElement(CopyTitle, {
      title: dialogInfo.title,
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    height: 600,
    visible: dialogInfo.visible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/React.createElement(_Button, {
      onClick: onDetailOk,
      type: "primary"
    }, t('setter.ok')), /*#__PURE__*/React.createElement(_Button, {
      style: {
        marginLeft: '10px'
      },
      onClick: closeDialog
    }, t('setter.cancel'))]
  }, /*#__PURE__*/React.createElement(ConditionRender, {
    type: conditionType || ConditionType.all,
    selectRelate: selectRelate || SelectRelateType.addAndOr,
    selectRowRelate: selectRowRelate || SelectRelateType.all,
    data: dialogInfo.detailConfig.data,
    onChange: onConditionRenderChange
  })));
};
var _default = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  _inheritsLoose(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/React.createElement(ConditionDialogRender, this.props);
  };
  return _default;
}(Component);
export { _default as default };