"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _react = _interopRequireWildcard(require("react"));
var _boxSlotComponent = _interopRequireDefault(require("../../../../_components/boxSlotComponent"));
var _coreConnectorBeta = require("@cverse/core-connector-beta");
var _coreLibBeta = require("@cverse/core-lib-beta");
var _context3 = require("../../../context");
var _rulePanelService = require("../../service/rulePanelService");
var _ = require("../../../..");
var _utils = require("../../../../cBehavior-setter/config/utils");
var _utils2 = require("../../utils");
var _locale = require("../../../../../../src/locale");
var _schemaService = require("../../../../../_utils/schemaService");
var _type = require("../../../../cGroupManage-setter/type");
var _treeContext = require("../../../../cDataSourceList-setter/treeContext");
var _utils3 = require("../../../../../_utils/utils");
var _PbiDataSource = require("../../../../cDataSource-setter/commonFn/PbiDataSource");
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 projectApi = new _coreLibBeta.ProjectApi();
var pluginApi = new _coreLibBeta.PluginApi();
var _useWalletConnect = (0, _coreConnectorBeta.useWalletConnect)(),
  walletConnect = _useWalletConnect.walletConnect;
var selectArrOrigin = (0, _utils2.getOriginOptions)();
var pbiDataSource = new _PbiDataSource.PbiDataSource();
var RuleSetterPanel = function RuleSetterPanel(props) {
  var _current$config, _current$config2, _current$config3, _current$config4, _current$config5, _current$config6, _current$config7, _current$config8, _current$config9, _current$config10, _current$config11, _current$config12;
  var current = props.current,
    onChange = props.onChange;
  var FlowCtx = (0, _react.useContext)(_context3.FlowContext);
  var rulePanelService = new _rulePanelService.RulePanelService();
  var _useState = (0, _react.useState)([]),
    chainIdOption = _useState[0],
    setChainIdOption = _useState[1];
  var _useState2 = (0, _react.useState)([]),
    selectPbiOption = _useState2[0],
    setSelectPbiOption = _useState2[1];
  var _useState3 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config = current.config) === null || _current$config === void 0 ? void 0 : _current$config.chainId) || -1),
    chainId = _useState3[0],
    setChainId = _useState3[1];
  var _useState4 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config2 = current.config) === null || _current$config2 === void 0 ? void 0 : _current$config2.handlerName) || ''),
    handlerName = _useState4[0],
    setHandlerName = _useState4[1];
  var _useState5 = (0, _react.useState)(current === null || current === void 0 ? void 0 : (_current$config3 = current.config) === null || _current$config3 === void 0 ? void 0 : _current$config3.pbiId),
    pbiId = _useState5[0],
    setPbiId = _useState5[1];
  var _useState6 = (0, _react.useState)(current === null || current === void 0 ? void 0 : (_current$config4 = current.config) === null || _current$config4 === void 0 ? void 0 : _current$config4.ruleId),
    ruleId = _useState6[0],
    setRuleId = _useState6[1];
  var _useState7 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config5 = current.config) === null || _current$config5 === void 0 ? void 0 : _current$config5.viewKey) || ""),
    viewKey = _useState7[0],
    setViewKey = _useState7[1];
  var _useState8 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config6 = current.config) === null || _current$config6 === void 0 ? void 0 : _current$config6.pageKey) || ""),
    pageKey = _useState8[0],
    setPageKey = _useState8[1];
  var _useState9 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config7 = current.config) === null || _current$config7 === void 0 ? void 0 : _current$config7.apiParams) || []),
    apiParams = _useState9[0],
    setApiParams = _useState9[1];
  var _useState10 = (0, _react.useState)((current === null || current === void 0 ? void 0 : (_current$config8 = current.config) === null || _current$config8 === void 0 ? void 0 : _current$config8.groupIds) || []),
    groupIds = _useState10[0],
    setGroupIds = _useState10[1];
  var _useState11 = (0, _react.useState)({
      pluginProjectId: current === null || current === void 0 ? void 0 : (_current$config9 = current.config) === null || _current$config9 === void 0 ? void 0 : _current$config9.pluginProjectId,
      pluginProjectViewId: current === null || current === void 0 ? void 0 : (_current$config10 = current.config) === null || _current$config10 === void 0 ? void 0 : _current$config10.pluginProjectViewId,
      projectId: (current === null || current === void 0 ? void 0 : (_current$config11 = current.config) === null || _current$config11 === void 0 ? void 0 : _current$config11.projectId) || FlowCtx.projectId || -1,
      pluginId: (current === null || current === void 0 ? void 0 : (_current$config12 = current.config) === null || _current$config12 === void 0 ? void 0 : _current$config12.pluginId) || FlowCtx.pluginId || -1
    }),
    otherParams = _useState11[0],
    setOtherParams = _useState11[1];
  var _useState12 = (0, _react.useState)([]),
    groupDataSource = _useState12[0],
    setGroupDataSource = _useState12[1];
  var _useState13 = (0, _react.useState)([]),
    viewList = _useState13[0],
    setViewList = _useState13[1];
  var _useState14 = (0, _react.useState)([]),
    viewKeyOptions = _useState14[0],
    setViewKeyOptions = _useState14[1];
  var _useState15 = (0, _react.useState)([]),
    pageKeyOptions = _useState15[0],
    setPageKeyOptions = _useState15[1];
  (0, _react.useEffect)(function () {
    var chainIdList = (0, _utils3.getChainIdOptions)();
    setChainIdOption(chainIdList);
    getPbiOptions();
    var schemaService = new _schemaService.SchemaService();
    var gList = schemaService.getPageGroupListData(_type.groupCategoryType.allPurpose);
    setGroupDataSource(gList);
  }, []);
  (0, _react.useEffect)(function () {
    if (ruleId) {
      getViewJsonByRuleId(ruleId);
    }
  }, [ruleId]);
  (0, _react.useEffect)(function () {
    if (pbiId !== undefined) {
      var data = (0, _extends2["default"])({}, otherParams, {
        pbiId: pbiId,
        ruleId: ruleId,
        viewKey: viewKey,
        pageKey: pageKey,
        apiParams: apiParams,
        groupIds: groupIds,
        chainId: chainId
      });
      var obj = (0, _extends2["default"])({}, current, {
        config: data
      });
      onChange(obj);
    }
  }, [chainId, otherParams, pbiId, ruleId, viewKey, pageKey, apiParams, groupIds]);
  var getPbiOptions = /*#__PURE__*/function () {
    var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee(chainIdData) {
      var newChainId, res;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            _context.next = 2;
            return pbiDataSource.initPbiService();
          case 2:
            newChainId = chainIdData || chainId;
            res = pbiDataSource.getPbiListType(newChainId);
            setSelectPbiOption(res);
          case 5:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    return function getPbiOptions(_x) {
      return _ref.apply(this, arguments);
    };
  }();
  var getViewJsonByRuleId = /*#__PURE__*/function () {
    var _ref2 = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee2(ruleId) {
      var _res$viewJson;
      var res, list;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            _context2.next = 2;
            return pbiDataSource.getViewJsonByRuleId(ruleId);
          case 2:
            res = _context2.sent;
            if (res !== null && res !== void 0 && (_res$viewJson = res.viewJson) !== null && _res$viewJson !== void 0 && _res$viewJson.viewList) {
              list = rulePanelService.getViewKeyOptionsByViewList(res.viewJson.viewList);
              setViewList(res.viewJson.viewList);
              setViewKeyOptions(list);
            } else {
              console.error('request error ~~');
            }
          case 4:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    return function getViewJsonByRuleId(_x2) {
      return _ref2.apply(this, arguments);
    };
  }();
  var handleInstanceOtherData = function handleInstanceOtherData(item) {
    setOtherParams((0, _extends2["default"])({}, otherParams, {
      pluginProjectId: item.pluginProjectId,
      pluginProjectViewId: item.pluginProjectViewId,
      projectId: FlowCtx.projectId || -1,
      pluginId: FlowCtx.pluginId || -1
    }));
  };
  var updatePageKeyOptions = function updatePageKeyOptions(key) {
    var list = rulePanelService.getPageKeyOptionsByViewList(viewList, key);
    setPageKeyOptions(list);
  };
  var updateInitParams = function updateInitParams(viewKey, pageKey) {
    if (viewList.length && viewKey.length && pageKey.length) {
      var params = rulePanelService.getParamsOptionsByViewList(viewList, viewKey, pageKey);
      setApiParams(params);
    }
  };
  var clearOther = function clearOther(keep) {
    if (keep === 0) {
      setPbiId(undefined);
      setRuleId(undefined);
      setViewKey("");
      setPageKey("");
      setApiParams([]);
    } else if (keep === 1) {
      setViewKey("");
      setPageKey("");
      setApiParams([]);
    } else if (keep === 2) {
      setPageKey("");
      setApiParams([]);
    }
  };
  return /*#__PURE__*/_react["default"].createElement("div", {
    className: "rule-box"
  }, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.chainIdData")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: chainIdOption,
    value: chainId,
    onChange: function onChange(v) {
      setChainId(v);
      clearOther(0);
      getPbiOptions(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.ruleSelect")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: selectPbiOption,
    value: pbiId,
    onChange: function onChange(v, type, item) {
      clearOther(1);
      setPbiId(v);
      setHandlerName(item.handlerName || '');
      setRuleId(item.ruleId);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.viewKeyTitle")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: viewKeyOptions,
    value: viewKey,
    onChange: function onChange(v) {
      setViewKey(v);
      clearOther(3);
      updatePageKeyOptions(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.pageKeyTitle")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: pageKeyOptions,
    value: pageKey,
    onChange: function onChange(v) {
      setPageKey(v);
      updateInitParams(viewKey, v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.groupId")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: groupDataSource,
    value: groupIds,
    mode: "multiple",
    onChange: function onChange(v) {
      setGroupIds(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.apiParamsTitle")
  }, /*#__PURE__*/_react["default"].createElement(_treeContext.CTreeContext.Provider, {
    value: {
      paramsOpen: true,
      isShowAddBtn: true
    }
  }, /*#__PURE__*/_react["default"].createElement(_.CTreeDataRelationSetter, {
    key: (0, _utils.getNewKeyByApiParams)(apiParams),
    value: apiParams,
    maxAddNum: -1,
    onChange: function onChange(e) {
      setApiParams(e);
    }
  }))));
};
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(RuleSetterPanel, this.props);
  };
  return _default;
}(_react.Component);