"use strict";

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = exports.initialState = void 0;

var _immutable = require("immutable");

var _jsCookie = _interopRequireDefault(require("js-cookie"));

var _kit_utils = require("kit_utils");

var _defaultSetting = _interopRequireDefault(require("../../../../config/defaultSetting"));

var _routes = _interopRequireWildcard(require("../../../utils/routes"));

var _storage = _interopRequireDefault(require("../../../utils/storage"));

var _constants = require("./constants");

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var keywordStorage = new _storage["default"]("__global_search_keywords__micro_".concat(window.location.hostname));
var recentVisitedStorage = new _storage["default"]("__global_recent_visited__micro_".concat(window.location.hostname));
var recentProductStorage = new _storage["default"]("__global__recent_product__micro_".concat(window.location.hostname));
var currentProduct = new _kit_utils.Storeage("__global__recent_productIndex__micro_".concat(window.location.hostname));
var initialState = (0, _immutable.fromJS)({
  globalConfig: {},
  globalConfigError: '',
  currentRoutes: {},
  instance: null,
  menu: [],
  flatMenu: [],
  defaultSetting: _defaultSetting["default"],
  recentKeywords: keywordStorage.list || [],
  recentVisited: recentVisitedStorage.list || [],
  recentProducts: recentProductStorage.list || [],
  currentMenuIndex: currentProduct.get("currentMenuIndex") || 0,
  currentMenu: null,
  collapsed: true,
  loginMode: _jsCookie["default"].get("loginMode") || 'pdw',
  authValidate: false,
  pathname: '',
  selectedKeys: null,
  openKeys: [],
  complexConfig: {},
  complexPathName: null
});
/* eslint-disable default-case, no-param-reassign */

exports.initialState = initialState;

var appReducer = function appReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  var action = arguments.length > 1 ? arguments[1] : undefined;

  switch (action.type) {
    case _constants.GET_CONFIG:
      window.__INITIAL_STATE__ = Object.assign({}, action.data, {
        allWidgets: action.data.widgets || []
      });
      window.globalConfig = require('kit_global_config')["default"];
      createStoreage.set('globalConfig', window.globalConfig);
      var config = createStoreage.get('globalConfig');
      var generMenu = (0, _routes["default"])(config.widgets || []);
      var flatMenu = (0, _routes.generFlatRoutes)(generMenu || []);
      return state.merge({
        globalConfig: config,
        menu: generMenu,
        flatMenu: flatMenu,
        currentMenu: generMenu[state.currentMenuIndex]
      });

    case _constants.GET_ERROR_CONFIG:
      return state.merge({
        globalConfigError: action.data
      });

    case _constants.GET_CURRENT_ROUTES:
      return state.merge({
        currentRoutes: action.data
      });

    case _constants.CREATE_CONEXT:
      return state.merge({
        instance: action.data
      });

    case _constants.GENERATE_MENU:
      return state.merge({
        menu: action.data,
        currentMenu: action.data[state.currentMenuIndex]
      });

    case _constants.GET_THEME_CONFIG:
      return state.merge({
        defaultSetting: action.data
      });

    case _constants.GET_RCENT_KEYWORD:
      keywordStorage.set('keywords', action.data);
      return state.merge({
        recentKeywords: keywordStorage.list
      });

    case _constants.GET_RECENT_VISITED:
      recentVisitedStorage.set('history', action.data);
      return state.merge({
        recentVisited: recentVisitedStorage.list
      });

    case _constants.GET_RECENT_PRODUCT:
      recentProductStorage.set('history', action.data);
      return state.merge({
        recentProducts: recentProductStorage.list
      });

    case _constants.CLEAR_RCENT_KEYWORD:
      keywordStorage.clear();
      return state.merge({
        recentKeywords: [],
        recentVisited: []
      });

    case _constants.GET_CURRENT_INDEX:
      currentProduct.set("currentMenuIndex", action.data);
      return state.merge({
        currentMenuIndex: currentProduct.get("currentMenuIndex")
      });

    case _constants.SLIDER_MENU_MODE:
      return state.merge({
        collapsed: action.data
      });

    case _constants.LOGIN_MODE:
      return state.merge({
        loginMode: action.data
      });

    case _constants.VALIDATE_AUTH:
      return state.merge({
        authValidate: action.data
      });

    case _constants.GET_PATHNAME:
      return state.merge({
        pathname: action.data
      });

    case _constants.SELECT_MENU_KEY:
      debugger;
      return state.merge({
        selectedKeys: action.data
      });

    case _constants.EXTEND_OPEN_KEY:
      return state.merge({
        openKeys: action.data
      });

    case _constants.COMPLEX_CONFIG:
      return state.merge({
        complexConfig: action.data
      });

    case _constants.COMPLEX_PATHNAME:
      return state.merge({
        complexPathName: action.data
      });

    default:
      return state;
  }
};

var _default = appReducer;
exports["default"] = _default;