"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 _locale = require("../../../src/locale");
require("./index.less");
var _type = require("../../_utils/conditionRenderService/type");
var _conditionRenderService = require("../../_utils/conditionRenderService");
var _lodash = require("lodash");
var _copyTitle = _interopRequireDefault(require("../_components/copyTitle"));
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 conditionRenderService = new _conditionRenderService.ConditionRenderService();

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

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

  /**
   * 导出关系配置信息
   */
  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.dependRelation"),
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    visible: dialogVisible,
    onClose: closeDialog,
    footer: [/*#__PURE__*/_react["default"].createElement(_button["default"], {
      onClick: handleOk,
      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: 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(CComputedSetter, this.props);
  };
  return _default;
}(_react.Component);