import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Button from "@alifd/next/es/button";
import _Dialog from "@alifd/next/es/dialog";
/**
 * 不展示条件后期也可以使用事件流逐渐替换
 * 后期可和cComputed-setter 整合成一个
 */
import React, { Component, useEffect, useState } from "react";
import ConditionRender from "../_components/conditionRender";
import { ConditionType, SelectRelateType } from "../../_utils/conditionRenderService/type";
import { ConditionRenderService } from "../../_utils/conditionRenderService";
import { isObject } from "lodash";
import CopyTitle from "../_components/copyTitle";
import { t } from "../../../src/locale";
var conditionRenderService = new ConditionRenderService();
var CNoDisplayConditionSetter = function CNoDisplayConditionSetter(props) {
  var type = ConditionType.display;
  var selectRelate = SelectRelateType.addAndOr;
  var selectRowRelate = SelectRelateType.compare;
  var _useState = useState(false),
    dialogVisible = _useState[0],
    setDialogVisible = _useState[1];
  var _useState2 = useState([]),
    conditions = _useState2[0],
    setConditions = _useState2[1];
  useEffect(function () {
    initData();
  }, []);
  function initData() {
    var result = [];
    if (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.confirm({
      v2: true,
      title: /*#__PURE__*/React.createElement(React.Fragment, null, t("setter.displayTitle")),
      content: /*#__PURE__*/React.createElement(React.Fragment, null, t("setter.displayContent")),
      onOk: function onOk() {
        var data = conditionRenderService.createInitItemOneByType(ConditionType.computed);
        comOnChange([data]);
        setConditions([data]);
      }
    });
  }

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

  /**
   * 导入关系配置信息
   */
  function importComputed(data) {
    setConditions(data);
  }
  return /*#__PURE__*/React.createElement("div", {
    className: "CComputedBox"
  }, /*#__PURE__*/React.createElement(_Button, {
    style: {
      marginRight: 10
    },
    type: "primary",
    onClick: function onClick() {
      setDialogVisible(true);
    }
  }, t("setter.displayDisposition")), /*#__PURE__*/React.createElement(_Button, {
    onClick: function onClick() {
      resetCondition();
    }
  }, t("setter.displayReset")), /*#__PURE__*/React.createElement(_Dialog, {
    className: "CComputed-dialog",
    title: /*#__PURE__*/React.createElement(CopyTitle, {
      title: 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.createElement(ConditionRender, {
    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 = /*#__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(CNoDisplayConditionSetter, this.props);
  };
  return _default;
}(Component);
export { _default as default };