import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Button from "@alifd/next/es/button";
import _Dialog from "@alifd/next/es/dialog";
import React, { Component, useEffect, useState } from "react";
import ConditionRender from "../_components/conditionRender";
import { t } from "../../../src/locale";
import "./index.less";
import { ConditionType, SelectRelateType } from "../../_utils/conditionRenderService/type";
import { ConditionRenderService } from "../../_utils/conditionRenderService";
import { isObject } from "lodash";
import CopyTitle from "../_components/copyTitle";
var conditionRenderService = new ConditionRenderService();

/**
 * 需要将操作依赖项生成schema
 * @param props
 * @returns
 */

var CComputedSetter = function CComputedSetter(props) {
  var type = ConditionType.all;
  var selectRelate = SelectRelateType.all;
  var selectRowRelate = SelectRelateType.all;
  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 resetOk() {
    var data = conditionRenderService.createInitItemOneByType(ConditionType.computed);
    comOnChange([data]);
    setConditions([data]);
  }
  var dc;
  function resetCondition() {
    dc = _Dialog.confirm({
      v2: true,
      title: t("setter.displayTitle"),
      content: t("setter.displayContent"),
      footer: /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Button, {
        onClick: resetOk,
        type: "primary"
      }, t('setter.ok')), /*#__PURE__*/React.createElement(_Button, {
        style: {
          marginLeft: '10px'
        },
        onClick: function onClick() {
          var _dc;
          (_dc = dc) === null || _dc === void 0 ? void 0 : _dc.hide();
        }
      }, t('setter.cancel')))
    });
  }

  /**
   * 导出关系配置信息
   */
  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.dependRelation"),
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    visible: dialogVisible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/React.createElement(_Button, {
      onClick: handleOk,
      type: "primary"
    }, t('setter.ok')), /*#__PURE__*/React.createElement(_Button, {
      style: {
        marginLeft: '10px'
      },
      onClick: closeDialog
    }, t('setter.cancel'))]
  }, /*#__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(CComputedSetter, this.props);
  };
  return _default;
}(Component);
export { _default as default };