"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Unstable_StatefulContainer = Unstable_StatefulContainer;

var React = _interopRequireWildcard(require("react"));

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

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; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { 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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function useDuplicateColumnTitleWarning(columns) {
  React.useEffect(function () {
    if (process.env.NODE_ENV !== "production") {
      var titles = columns.reduce(function (set, column) {
        return set.add(column.title);
      }, new Set());

      if (titles.size < columns.length) {
        console.warn('BaseWeb DataTable: Column titles must be unique else will result in non-deterministic filtering.');
      }
    }
  }, [columns]);
}

function useSortParameters() {
  var _React$useState = React.useState(-1),
      _React$useState2 = _slicedToArray(_React$useState, 2),
      sortIndex = _React$useState2[0],
      setSortIndex = _React$useState2[1];

  var _React$useState3 = React.useState(null),
      _React$useState4 = _slicedToArray(_React$useState3, 2),
      sortDirection = _React$useState4[0],
      setSortDirection = _React$useState4[1];

  function handleSort(columnIndex) {
    if (columnIndex === sortIndex) {
      if (sortDirection === _constants.SORT_DIRECTIONS.ASC) {
        setSortIndex(-1);
        setSortDirection(_constants.SORT_DIRECTIONS.DESC);
      } else {
        setSortDirection(_constants.SORT_DIRECTIONS.ASC);
      }
    } else {
      setSortIndex(columnIndex);
      setSortDirection(_constants.SORT_DIRECTIONS.DESC);
    }
  }

  return [sortIndex, sortDirection, handleSort];
}

function Unstable_StatefulContainer(props) {
  useDuplicateColumnTitleWarning(props.columns);

  var _useSortParameters = useSortParameters(),
      _useSortParameters2 = _slicedToArray(_useSortParameters, 3),
      sortIndex = _useSortParameters2[0],
      sortDirection = _useSortParameters2[1],
      handleSort = _useSortParameters2[2];

  var _React$useState5 = React.useState(new Map()),
      _React$useState6 = _slicedToArray(_React$useState5, 2),
      filters = _React$useState6[0],
      setFilters = _React$useState6[1];

  var _React$useState7 = React.useState(''),
      _React$useState8 = _slicedToArray(_React$useState7, 2),
      textQuery = _React$useState8[0],
      setTextQuery = _React$useState8[1];

  function handleFilterAdd(filterParams, title) {
    filters.set(title, filterParams);
    setFilters(new Map(filters));
  }

  function handleFilterRemove(title) {
    filters.delete(title);
    setFilters(new Map(filters));
  }

  var _React$useState9 = React.useState(new Set()),
      _React$useState10 = _slicedToArray(_React$useState9, 2),
      selectedRowIds = _React$useState10[0],
      setSelectedRowIds = _React$useState10[1];

  function handleSelectChange(next) {
    setSelectedRowIds(next);
    var selectionCallback = props.onSelectionChange;

    if (selectionCallback) {
      selectionCallback(props.rows.filter(function (r) {
        return next.has(r.id);
      }));
    }
  }

  function handleSelectMany(incomingRows) {
    // only adds rows that are visible in the table
    handleSelectChange(new Set([].concat(_toConsumableArray(selectedRowIds), _toConsumableArray(incomingRows.map(function (r) {
      return r.id;
    })))));
  }

  function handleSelectNone() {
    handleSelectChange(new Set());
  }

  function handleSelectOne(row) {
    if (selectedRowIds.has(row.id)) {
      selectedRowIds.delete(row.id);
    } else {
      selectedRowIds.add(row.id);
    }

    handleSelectChange(new Set(selectedRowIds));
  }

  return props.children({
    filters: filters,
    onFilterAdd: handleFilterAdd,
    onFilterRemove: handleFilterRemove,
    onSelectMany: handleSelectMany,
    onSelectNone: handleSelectNone,
    onSelectOne: handleSelectOne,
    onTextQueryChange: setTextQuery,
    onSort: handleSort,
    selectedRowIds: selectedRowIds,
    sortIndex: sortIndex,
    sortDirection: sortDirection,
    textQuery: textQuery
  });
}