import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _Select from "@alifd/next/es/select";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _extends from "@babel/runtime/helpers/extends";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import React, { Component, useContext, useEffect, useState } from "react";
import BoxSlotComponent from "../../../../_components/boxSlotComponent";
import { useWalletConnect } from "@cverse/core-connector-beta";
import { ProjectApi, PluginApi } from "@cverse/core-lib-beta";
import { FlowContext } from "../../../context";
import { RulePanelService } from "../../service/rulePanelService";
import { CTreeDataRelationSetter } from "../../../..";
import { getNewKeyByApiParams } from "../../../../cBehavior-setter/config/utils";
import { getOriginOptions } from "../../utils";
import { t } from "../../../../../../src/locale";
import { SchemaService } from "../../../../../_utils/schemaService";
import { groupCategoryType } from "../../../../cGroupManage-setter/type";
import { CTreeContext } from "../../../../cDataSourceList-setter/treeContext";
import { getChainIdOptions } from "../../../../../_utils/utils";
import { PbiDataSource } from "../../../../cDataSource-setter/commonFn/PbiDataSource";
var projectApi = new ProjectApi();
var pluginApi = new PluginApi();
var _useWalletConnect = useWalletConnect(),
  walletConnect = _useWalletConnect.walletConnect;
var selectArrOrigin = getOriginOptions();
var pbiDataSource = new 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 = useContext(FlowContext);
  var rulePanelService = new RulePanelService();
  var _useState = useState([]),
    chainIdOption = _useState[0],
    setChainIdOption = _useState[1];
  var _useState2 = useState([]),
    selectPbiOption = _useState2[0],
    setSelectPbiOption = _useState2[1];
  var _useState3 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = 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 = useState([]),
    groupDataSource = _useState12[0],
    setGroupDataSource = _useState12[1];
  var _useState13 = useState([]),
    viewList = _useState13[0],
    setViewList = _useState13[1];
  var _useState14 = useState([]),
    viewKeyOptions = _useState14[0],
    setViewKeyOptions = _useState14[1];
  var _useState15 = useState([]),
    pageKeyOptions = _useState15[0],
    setPageKeyOptions = _useState15[1];
  useEffect(function () {
    var chainIdList = getChainIdOptions();
    setChainIdOption(chainIdList);
    getPbiOptions();
    var schemaService = new SchemaService();
    var gList = schemaService.getPageGroupListData(groupCategoryType.allPurpose);
    setGroupDataSource(gList);
  }, []);
  useEffect(function () {
    if (ruleId) {
      getViewJsonByRuleId(ruleId);
    }
  }, [ruleId]);
  useEffect(function () {
    if (pbiId !== undefined) {
      var data = _extends({}, otherParams, {
        pbiId: pbiId,
        ruleId: ruleId,
        viewKey: viewKey,
        pageKey: pageKey,
        apiParams: apiParams,
        groupIds: groupIds,
        chainId: chainId
      });
      var obj = _extends({}, current, {
        config: data
      });
      onChange(obj);
    }
  }, [chainId, otherParams, pbiId, ruleId, viewKey, pageKey, apiParams, groupIds]);
  var getPbiOptions = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(chainIdData) {
      var newChainId, res;
      return _regeneratorRuntime.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 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(ruleId) {
      var _res$viewJson;
      var res, list;
      return _regeneratorRuntime.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(_extends({}, 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.createElement("div", {
    className: "rule-box"
  }, /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.chainIdData")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: chainIdOption,
    value: chainId,
    onChange: function onChange(v) {
      setChainId(v);
      clearOther(0);
      getPbiOptions(v);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.ruleSelect")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: 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.createElement(BoxSlotComponent, {
    title: t("setter.viewKeyTitle")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: viewKeyOptions,
    value: viewKey,
    onChange: function onChange(v) {
      setViewKey(v);
      clearOther(3);
      updatePageKeyOptions(v);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.pageKeyTitle")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: pageKeyOptions,
    value: pageKey,
    onChange: function onChange(v) {
      setPageKey(v);
      updateInitParams(viewKey, v);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.groupId")
  }, /*#__PURE__*/React.createElement(_Select, {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: t("setter.selectPlaceHolder"),
    notFoundContent: t('setter.notFoundContent'),
    dataSource: groupDataSource,
    value: groupIds,
    mode: "multiple",
    onChange: function onChange(v) {
      setGroupIds(v);
    }
  })), /*#__PURE__*/React.createElement(BoxSlotComponent, {
    title: t("setter.apiParamsTitle")
  }, /*#__PURE__*/React.createElement(CTreeContext.Provider, {
    value: {
      paramsOpen: true,
      isShowAddBtn: true
    }
  }, /*#__PURE__*/React.createElement(CTreeDataRelationSetter, {
    key: getNewKeyByApiParams(apiParams),
    value: apiParams,
    maxAddNum: -1,
    onChange: function onChange(e) {
      setApiParams(e);
    }
  }))));
};
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(RuleSetterPanel, this.props);
  };
  return _default;
}(Component);
export { _default as default };