'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
require('core-js/modules/es.symbol.js');
require('core-js/modules/es.array.filter.js');
require('core-js/modules/es.array.find-index.js');
require('core-js/modules/es.object.get-own-property-descriptor.js');
require('core-js/modules/es.object.get-own-property-descriptors.js');
require('core-js/modules/es.object.keys.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.regexp.exec.js');
require('core-js/modules/es.string.search.js');
require('core-js/modules/web.dom-collections.for-each.js');
var tableReducer = require('../Table/tableReducer.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty__default.default(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var TILE_ACTIONS = {
  PAGE_CHANGE: 'PAGE_CHANGE',
  SEARCH: 'SEARCH',
  SELECT: 'SELECT',
  RESET: 'RESET'
};

/** figures out the ending index of the array */
var determineEndingIndex = function determineEndingIndex(page, pageSize) {
  return (page - 1) * pageSize + pageSize - 1;
};

/** This figures out the initial state of the reducer from the props */
var determineInitialState = function determineInitialState(_ref) {
  var pagination = _ref.pagination,
    _ref$isSelectedByDefa = _ref.isSelectedByDefault,
    isSelectedByDefault = _ref$isSelectedByDefa === void 0 ? true : _ref$isSelectedByDefa,
    search = _ref.search,
    selectedTileId = _ref.selectedTileId,
    tiles = _ref.tiles;
  var page = pagination && pagination.page ? pagination.page : 1;
  var pageSize = pagination && pagination.pageSize ? pagination.pageSize : 10;
  var filteredTiles = search ? tableReducer.searchData(tiles, search.value) : tiles;
  var startingIndex = pagination ? (page - 1) * pageSize : 0;
  var selectedTileIdState = selectedTileId || (isSelectedByDefault && filteredTiles && filteredTiles[startingIndex] ? filteredTiles[startingIndex].id : null);
  var selectedPage;
  var selectedStartingIndex;
  // If a selected tile id is passed, we should page to it
  if (selectedTileId) {
    var selectedTileIndex = filteredTiles.findIndex(function (tile) {
      return tile.id === selectedTileId;
    });
    selectedPage = Math.floor(selectedTileIndex / pageSize) + 1;
    selectedStartingIndex = pagination ? (selectedPage - 1) * pageSize : 0;
  }
  return {
    page: selectedPage || page,
    pageSize: pageSize,
    searchState: search && search.value ? search.value : '',
    selectedTileId: selectedTileIdState,
    tiles: tiles,
    // filtered tiles have any search applied
    filteredTiles: filteredTiles,
    startingIndex: selectedStartingIndex || startingIndex,
    endingIndex: determineEndingIndex(selectedPage || page, pageSize)
  };
};

/** here's what the state looks like
 * 
 *  { 
 *  page: PropTypes.number,
    pageSize: PropTypes.number,
    searchState: PropTypes.string,
    selectedTileId: PropTypes.string,
    // original tile data
    tiles: PropTypes.array,
    // filtered tiles have any search applied
    filteredTiles: PropTypes.array,
    startingIndex: PropTypes.number,
    endingIndex: PropTypes.number,
  }
 * 
 */
var tileCatalogReducer = function tileCatalogReducer() {
  var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var action = arguments.length > 1 ? arguments[1] : undefined;
  switch (action.type) {
    case TILE_ACTIONS.PAGE_CHANGE:
      {
        var pageSize = state.pageSize;
        var page = action.payload;
        var startingIndex = (page - 1) * pageSize;
        var endingIndex = determineEndingIndex(page, pageSize);
        return _objectSpread(_objectSpread({}, state), {}, {
          startingIndex: startingIndex,
          endingIndex: endingIndex,
          page: page
        });
      }
    case TILE_ACTIONS.SEARCH:
      {
        var searchState = action.payload;
        var _pageSize = state.pageSize;
        var filteredTiles = tableReducer.searchData(state.tiles, searchState);
        return _objectSpread(_objectSpread({}, state), {}, {
          searchState: searchState,
          filteredTiles: filteredTiles,
          // reset the page and indexes
          page: 1,
          startingIndex: 0,
          endingIndex: (_pageSize || filteredTiles.length) - 1
        });
      }
    case TILE_ACTIONS.SELECT:
      {
        var _filteredTiles = state.filteredTiles,
          _pageSize2 = state.pageSize;
        var tileIndex = _filteredTiles.findIndex(function (tile) {
          return tile.id === action.payload;
        });
        var _page = Math.floor(tileIndex / _pageSize2) + 1;
        return _objectSpread(_objectSpread({}, state), {}, {
          page: _page,
          startingIndex: (_page - 1) * _pageSize2,
          endingIndex: determineEndingIndex(_page, _pageSize2),
          selectedTileId: action.payload
        });
      }
    case TILE_ACTIONS.RESET:
      return determineInitialState(action.payload);
    default:
      return state;
  }
};

exports.TILE_ACTIONS = TILE_ACTIONS;
exports.determineInitialState = determineInitialState;
exports.tileCatalogReducer = tileCatalogReducer;
