"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.AlterStateComponent = void 0;
var _dialog = _interopRequireDefault(require("@alifd/next/lib/dialog"));
var _icon = _interopRequireDefault(require("@alifd/next/lib/icon"));
var _button = _interopRequireDefault(require("@alifd/next/lib/button"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _boxSlotComponent = _interopRequireDefault(require("../../_components/boxSlotComponent"));
var _renderByFormKey = _interopRequireDefault(require("../../_components/renderByFormKey"));
var _formKeyType = require("../../../_utils/formKeyType");
var _type = require("../../../_utils/conditionRenderService/type");
var _conditionRender = _interopRequireDefault(require("../../_components/conditionRender"));
var _alterStateService = require("../config/alterStateService");
require("../index.less");
var _copyTitle = _interopRequireDefault(require("../../_components/copyTitle"));
var _locale = require("../../../../src/locale");
var _type2 = require("../../../../src/utils/type");
var _lodash = require("lodash");
var _config = require("../config");
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 alterStateService = new _alterStateService.AlterStateService();
var AlterStateComponent = exports.AlterStateComponent = function AlterStateComponent(props) {
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)(_type2.EncryptionType["default"]),
    encryptionType = _useState[0],
    setEncryptionType = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    logicalData = _useState2[0],
    setLogicalData = _useState2[1];
  var _useState3 = (0, _react.useState)(false),
    alterDialog = _useState3[0],
    setAlterDialog = _useState3[1];

  // 兼容之前是数据结构
  (0, _react.useEffect)(function () {
    var res = initAlterStateData(value);
    setEncryptionType(res.type);
    if (res.logicalData) {
      setLogicalData(res.logicalData);
    }
  }, []);
  (0, _react.useEffect)(function () {
    var obj = {
      type: encryptionType,
      logicalData: logicalData
    };
    onChange(obj);
  }, [encryptionType, logicalData]);
  var _useState4 = (0, _react.useState)({
      visible: false,
      title: (0, _locale.t)("setter.alterValueConfig"),
      id: "",
      config: {
        data: [],
        resultData: {
          isHandle: false,
          resultRowList: []
        }
      }
    }),
    dialogInfo = _useState4[0],
    setDialogInfo = _useState4[1];
  function initAlterStateData(value) {
    var result = {
      type: _type2.EncryptionType["default"],
      logicalData: []
    };
    if ((0, _lodash.isArray)(value)) {
      result.logicalData = value;
    } else if (value.type) {
      result = value;
    }
    return result;
  }
  function addSelectOne() {
    var newData = alterStateService.addRowData(logicalData);
    setLogicalData(newData);
  }
  function copyRowData(index) {
    var newData = alterStateService.copyRowData(logicalData, index);
    setLogicalData(newData);
  }
  function removeRowData(id) {
    var newData = alterStateService.removeRowData(logicalData, id);
    setLogicalData(newData);
  }
  function showConditionPage(type, id, data) {
    if (type === "setData") {
      setDialogInfo({
        visible: true,
        title: (0, _locale.t)("setter.alterValueConfig"),
        id: id,
        config: data
      });
    } else {
      var newData = alterStateService.resetRowData(logicalData, id);
      setLogicalData([].concat(newData));
    }
  }
  function closeDialog() {
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.visible = false;
    setDialogInfo(newDialog);
  }
  function onDetailOk() {
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.visible = false;
    var newData = alterStateService.alterRowData({
      oldData: logicalData,
      id: newDialog.id,
      logic: newDialog.config
    });
    setLogicalData(newData);
    setDialogInfo(newDialog);
  }
  function onConditionRenderChange(obj) {
    var key = obj.key,
      value = obj.value;
    var newDialog = Object.assign({}, dialogInfo);
    newDialog.config[key] = value;
    setDialogInfo(newDialog);
  }
  function testChange(list, alterItem) {
    var newItem = {};
    list.forEach(function (item) {
      var key = item.key,
        value = item.value;
      newItem[key] = value;
    });
    alterItem.alterValue = (0, _extends2["default"])({}, alterItem.alterValue, newItem);
    var newData = alterStateService.alterRowData({
      oldData: logicalData,
      id: alterItem.id,
      value: alterItem.alterValue
    });
    setLogicalData(newData);
  }

  /**
   * 导出关系配置信息
   */
  function exportComputed() {
    return alterStateService.getCopyAlterStateList(logicalData);
  }

  /**
   * 导入关系配置信息
   */
  function importComputed(data) {
    setLogicalData(data);
  }
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "alterStateMainBox"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.encryptionType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    showSearch: true,
    value: encryptionType,
    dataSource: (0, _config.getEncryptionOptionsList)(),
    onChange: function onChange(e) {
      setEncryptionType(e);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: ""
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    onClick: function onClick() {
      setAlterDialog(true);
    }
  }, (0, _locale.t)("setter.alterDisposition"))), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "logicDetailBox",
    title: /*#__PURE__*/_react["default"].createElement(_copyTitle["default"], {
      title: (0, _locale.t)("setter.alterContent"),
      getExportStrData: exportComputed,
      getImportStrData: importComputed
    }),
    footerAlign: "right",
    height: 600,
    visible: alterDialog,
    onClose: function onClose() {
      setAlterDialog(false);
    },
    footer: false
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "alterStateBox"
  }, logicalData.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", {
      key: item.id,
      className: "alterRow"
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "alterRowLeft"
    }, index === 0 && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        color: "#fff"
      }
    }, (0, _locale.t)("setter.alterData")), /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
      relationOption: (0, _formKeyType.getAlterRelationList)(),
      value: {
        id: item.id,
        formKey: item.alterValue.formKey || _formKeyType.DataSourceType.defaultType,
        data: item.alterValue.data,
        treeData: item.alterValue.treeData || []
      },
      onChange: function onChange(data) {
        testChange(data, item);
      }
    })), /*#__PURE__*/_react["default"].createElement("div", null, index === 0 && /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        color: "#fff"
      }
    }, (0, _locale.t)("setter.setAlterLogic")), /*#__PURE__*/_react["default"].createElement("div", {
      className: "alterBtn"
    }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        marginRight: 10
      },
      type: "primary",
      onClick: function onClick() {
        showConditionPage("setData", item.id, item.alterLogic);
      }
    }, (0, _locale.t)("setter.setKey")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      style: {
        marginRight: 10
      },
      onClick: function onClick() {
        showConditionPage("resetData", item.id, item.alterLogic);
      }
    }, (0, _locale.t)("setter.displayReset")), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      type: "primary",
      style: {
        marginRight: 10
      },
      onClick: function onClick() {
        copyRowData(index);
      }
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "copy"
    })), /*#__PURE__*/_react["default"].createElement(_button["default"], {
      type: "primary",
      warning: true,
      onClick: function onClick() {
        removeRowData(item.id);
      }
    }, /*#__PURE__*/_react["default"].createElement(_icon["default"], {
      type: "ashbin"
    })))));
  }), /*#__PURE__*/_react["default"].createElement("div", {
    className: "sub_btn"
  }, /*#__PURE__*/_react["default"].createElement(_button["default"], {
    text: true,
    type: "primary",
    onClick: addSelectOne
  }, /*#__PURE__*/_react["default"].createElement("span", null, (0, _locale.t)("setter.addData")))))), /*#__PURE__*/_react["default"].createElement(_dialog["default"], {
    className: "logicDetailBox",
    title: dialogInfo.title,
    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: _type.ConditionType.computed,
    selectRelate: _type.SelectRelateType.computed,
    selectRowRelate: _type.SelectRelateType.all,
    data: dialogInfo.config.data,
    isDictionary: true,
    onChange: onConditionRenderChange
  })));
};