import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { ConnectorStyle } from "./handleStyle";
import { ConnectorSchema } from "./schema";
import { PanelPubSub, setLocaleLang, EPanelQuery, Panel } from '@cverse/core-lib-vue-beta';
import { getNetworksCode } from "@cverse/core-config-beta";
import { SwitchNum } from "../comFn/comType";
import { getLangType } from "lowcode/_utils/utils";
var ePanelQuery = new EPanelQuery();
export var ConnectorServer = /*#__PURE__*/function () {
  function ConnectorServer() {
    this.ConnectorSchema = void 0;
    this.connectorStyle = void 0;
    this.props = void 0;
    this.isDesign = void 0;
    /**
     * 存在2位
     * 第一位是打隐藏
     * 第二位是钱包隐藏
     * 
     * 由于switchList 对于钱包隐藏存在三种状态，
     * 1：显示线包
     * 0：隐藏钱包 v-if 
     * 2: 隐藏钱包（display:none）
     */
    this.switchList = void 0;
    this.connectorStyle = new ConnectorStyle();
  }
  var _proto = ConnectorServer.prototype;
  _proto.load = function load(props, switchList) {
    this.connectorStyle.loadStyle(props, switchList);
    this.props = props;
    this.switchList = switchList;
    this.isDesign = props.__designMode === 'design';
  };
  _proto.PSSubscribePanelSaveJson = function PSSubscribePanelSaveJson(entrySchema) {
    var _this = this;
    if (entrySchema) {
      var entrySchemaObj = JSON.parse(entrySchema.toString());
      this.ConnectorSchema = new ConnectorSchema('Connector');
      PanelPubSub.PSSubscribePanelSaveJson(entrySchemaObj.instance.id, function (e) {
        _this.ConnectorSchema.handlePanelViewJson(e);
      });
    }
  };
  _proto.subscribePanelJson = /*#__PURE__*/function () {
    var _subscribePanelJson = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee(entrySchema, callback) {
      var _entrySchemaObj$entry, entrySchemaObj, panel, switchList, panelPubSub;
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) switch (_context.prev = _context.next) {
          case 0:
            if (!entrySchema) {
              _context.next = 7;
              break;
            }
            entrySchemaObj = JSON.parse(entrySchema.toString());
            _context.next = 4;
            return ePanelQuery.makePanelByInstance(entrySchemaObj.instance);
          case 4:
            panel = _context.sent;
            switchList = (entrySchemaObj === null || entrySchemaObj === void 0 ? void 0 : (_entrySchemaObj$entry = entrySchemaObj.entry) === null || _entrySchemaObj$entry === void 0 ? void 0 : _entrySchemaObj$entry.switchList) || [];
            if (panel) {
              panel.switchPage('viewKey1', "home");
              panelPubSub = new PanelPubSub(panel);
              panelPubSub.PSUnsubscribePanelPanelJson();
              panelPubSub.PSSubscribePanelPanelJson(function (jsonData) {
                var newPanel = new Panel();
                newPanel.load(jsonData);
                var attributeList = newPanel.getCurPageAttrListByWidgetKeyFromRoot("home");
                callback && callback({
                  instanceId: entrySchemaObj.instance.id,
                  attributeList: attributeList,
                  switchList: switchList
                });
              });
            }
          case 7:
          case "end":
            return _context.stop();
        }
      }, _callee);
    }));
    function subscribePanelJson(_x, _x2) {
      return _subscribePanelJson.apply(this, arguments);
    }
    return subscribePanelJson;
  }();
  _proto.subscribePanelByProps = /*#__PURE__*/function () {
    var _subscribePanelByProps = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee2(entrySchema, callback) {
      var _entrySchemaObj$entry2, entrySchemaObj, panel, switchList, attributeList;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) switch (_context2.prev = _context2.next) {
          case 0:
            if (!entrySchema) {
              _context2.next = 7;
              break;
            }
            entrySchemaObj = JSON.parse(entrySchema.toString());
            _context2.next = 4;
            return ePanelQuery.makePanelByInstance(entrySchemaObj.instance);
          case 4:
            panel = _context2.sent;
            switchList = (entrySchemaObj === null || entrySchemaObj === void 0 ? void 0 : (_entrySchemaObj$entry2 = entrySchemaObj.entry) === null || _entrySchemaObj$entry2 === void 0 ? void 0 : _entrySchemaObj$entry2.switchList) || [];
            if (panel) {
              panel.switchPage('viewKey1', "home");
              attributeList = panel.getCurPageAttrListByWidgetKeyFromRoot("home");
              callback && callback({
                instanceId: entrySchemaObj.instance.id,
                attributeList: attributeList,
                switchList: switchList
              });
            }
          case 7:
          case "end":
            return _context2.stop();
        }
      }, _callee2);
    }));
    function subscribePanelByProps(_x3, _x4) {
      return _subscribePanelByProps.apply(this, arguments);
    }
    return subscribePanelByProps;
  }();
  _proto.createLangListByProps = /*#__PURE__*/function () {
    var _createLangListByProps = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3(entrySchema, callback) {
      var entrySchemaObj, panel, list, _list$, choiceList, value, langOption, config;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) switch (_context3.prev = _context3.next) {
          case 0:
            if (!entrySchema) {
              _context3.next = 6;
              break;
            }
            entrySchemaObj = JSON.parse(entrySchema.toString());
            _context3.next = 4;
            return ePanelQuery.makePanelByInstance(entrySchemaObj.instance);
          case 4:
            panel = _context3.sent;
            if (panel) {
              panel.switchPage('viewKey1', "home");
              list = panel.getCurPageAttrListByKeyFromRoot("langList");
              if (list && list.length) {
                _list$ = list[0], choiceList = _list$.choiceList, value = _list$.value;
                langOption = this.getLangOptions(choiceList, value);
                config = {
                  options: langOption,
                  langListAttribute: list[0]
                };
                callback && callback(config);
              }
            }
          case 6:
          case "end":
            return _context3.stop();
        }
      }, _callee3, this);
    }));
    function createLangListByProps(_x5, _x6) {
      return _createLangListByProps.apply(this, arguments);
    }
    return createLangListByProps;
  }();
  _proto.getLangOptionsByPanelJson = function getLangOptionsByPanelJson(attributeList) {
    var result;
    var list = attributeList.filter(function (item) {
      return item.key === 'langList';
    });
    if (list && list.length) {
      var _list$2 = list[0],
        choiceList = _list$2.choiceList,
        value = _list$2.value;
      result = {
        langListAttribute: list[0],
        options: this.getLangOptions(choiceList, value)
      };
    }
    return result;
  };
  _proto.getLangOptions = function getLangOptions(choiceList, valueList) {
    var newLang = this.getLocalStorage();
    var result = [];
    if (choiceList && valueList) {
      choiceList.forEach(function (item) {
        if (valueList.includes(item.value)) {
          result.push({
            label: item.name.label[newLang],
            value: item.value
          });
        }
      });
    }
    return result;
  };
  _proto.changeLang = function changeLang(lang, langListAttribute) {
    setLocaleLang(lang);
    var choiceList = langListAttribute.choiceList,
      value = langListAttribute.value;
    return this.getLangOptions(choiceList, value);
  };
  _proto.getPropsInAttributeList = function getPropsInAttributeList(attributeList) {
    var result = {};
    attributeList.forEach(function (item) {
      result[item.key] = item.value;
    });
    return result;
  };
  _proto.getRenderProps = function getRenderProps() {
    var _this2 = this;
    return {
      logo: this.props.logo,
      logoToUrl: this.props.logoToUrl,
      visibleConnectorStyle: this.connectorStyle.getNavigatorBg(),
      visibleConnector: this.getVisibleConnectorBySwitchList(),
      titleStyle: this.connectorStyle.getTitleStyle(),
      webGid: this.props.webGid,
      fixedLayout: this.connectorStyle.getFlexedLayout(),
      visibleWalletStyle: this.getVisibleWalletBySwitchList(),
      mainNetworks: this.getNetWorks('main'),
      testNetworks: this.getNetWorks('test'),
      hiddenConnector: function hiddenConnector(key) {
        return _this2.hiddenConnector(key);
      },
      webTitle: function webTitle() {
        return _this2.getWebTitle();
      }
    };
  };
  _proto.getNetWorks = function getNetWorks(flag) {
    var result = flag === 'main' ? this.props.mainNetworks : this.props.testNetworks;
    // 说明钱包css隐藏,则需要配置全部链路
    if (this.isSwitchList() && this.switchList[1] === SwitchNum.cssHidden) {
      var networksObj = getNetworksCode();
      result = flag === 'main' ? networksObj.mainCode : networksObj.testCode;
    }
    return result;
  };
  _proto.hiddenConnector = function hiddenConnector(key) {
    var navigatorObj = {
      'lang': !this.props.hiddenConnectorLang,
      'logo': !this.props.hiddenConnectorLogo,
      'title': !this.props.hiddenConnectorTitle,
      'wallet': !this.props.hiddenConnectorWallet,
      'individualCenter': !this.props.hiddenIndividualCenter
    };
    if (navigatorObj[key] !== undefined) {
      var result = navigatorObj[key];
      if (this.isSwitchList() && key === 'wallet') {
        // 当存在1显示和2css隐藏的情况下，都应该先显示在用css控制
        result = this.switchList[1] !== SwitchNum.vIfHidden;
      }
      return result;
    }
    return true;
  }

  /**
   * 由于switchList 对于钱包隐藏存在三种状态，
   * 1：显示线包
   * 0：隐藏钱包 v-if
   * 2: 隐藏钱包（display:none）
   */;
  _proto.getVisibleWalletBySwitchList = function getVisibleWalletBySwitchList() {
    return this.isSwitchList() && this.switchList[1] === SwitchNum.cssHidden;
  };
  _proto.getVisibleConnectorBySwitchList = function getVisibleConnectorBySwitchList() {
    if (this.isDesign) return true;
    var result = !this.props.hiddenConnector;
    if (this.switchList && this.switchList.length > 0 && this.switchList[0] !== undefined) {
      result = this.switchList[0] != SwitchNum.vIfHidden;
    }
    return result;
  };
  _proto.isSwitchList = function isSwitchList() {
    return this.switchList && this.switchList.length > 0 && this.switchList[1] !== undefined;
  };
  _proto.getWebTitle = function getWebTitle() {
    var lang = this.getLocalStorage();
    if (this.props.webTitle && this.props.webTitle[lang] !== undefined) {
      return this.props.webTitle[lang];
    }
    return 'RuleOS';
  };
  _proto.getLocalStorage = function getLocalStorage() {
    return getLangType();
  };
  return ConnectorServer;
}();