"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 _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__");
var recentVisitedStorage = new _storage["default"]("__global_recent_visited__");
var recentProductStorage = new _storage["default"]("__global__recent_product__");
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: 0
});
/* 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
      });

    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
      });

    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: []
      });

    case _constants.GET_CURRENT_INDEX:
      return state.merge({
        currentMenuIndex: action.data
      });

    default:
      return state;
  }
};

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