import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { useState, useMemo } from 'react';
import { DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE } from '@bigbinary/neeto-commons-frontend/constants';
import { taxonomies } from '@bigbinary/neeto-commons-frontend/initializers';
import { useQueryParams, useFuncDebounce, useDebounce } from '@bigbinary/neeto-commons-frontend/react-utils';
import { buildFiltersFromURL } from '@bigbinary/neeto-filters-frontend/utils';
import { mergeLeft, isNil, assoc, pluck } from 'ramda';
import { useTranslation } from 'react-i18next';
import { b as useFetchFilterOptions } from './useTeamsApi-DkgWqw-g.js';
import { d as defaultNeetoFiltersColumns, D as DEFAULT_FILTER_VALUES, b as buildFilterColumns, a as DEFAULT_IS_OPEN_VALUES, c as DEFAULT_SELECTED_ROWS, h as handlePageChange } from './utils-3WlshQ9e.js';
import { useHistory } from 'react-router-dom';

function ownKeys$1(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$1(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys$1(Object(t), true).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys$1(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var useFilters = function useFilters(_ref) {
  var _config$filterColumns;
  var config = _ref.config;
  var _useTranslation = useTranslation(),
    t = _useTranslation.t;
  var _useQueryParams = useQueryParams(),
    _useQueryParams$searc = _useQueryParams.searchTerm,
    searchTerm = _useQueryParams$searc === void 0 ? "" : _useQueryParams$searc;
  var searchKeywordProps = {
    node: "first_name,last_name,email",
    value: searchTerm,
    key: "search_term"
  };
  var _useState = useState(""),
    _useState2 = _slicedToArray(_useState, 2),
    emailSearchTerm = _useState2[0],
    setEmailSearchTerm = _useState2[1];
  var defaultNeetoFilters = buildFiltersFromURL([].concat(_toConsumableArray((_config$filterColumns = config === null || config === void 0 ? void 0 : config.filterColumns) !== null && _config$filterColumns !== void 0 ? _config$filterColumns : defaultNeetoFiltersColumns()), [searchKeywordProps]));
  var _useState3 = useState(_objectSpread$1(_objectSpread$1(_objectSpread$1({}, DEFAULT_FILTER_VALUES), config === null || config === void 0 ? void 0 : config.defaultFilterValues), {}, {
      neetoFilters: defaultNeetoFilters
    })),
    _useState4 = _slicedToArray(_useState3, 2),
    filters = _useState4[0],
    setFilters = _useState4[1];
  var resetPage = useFuncDebounce(function () {
    return setFilters(mergeLeft({
      page: DEFAULT_PAGE_INDEX
    }));
  });
  var debouncedEmailSearchTerm = useDebounce(emailSearchTerm, 750);
  var shouldFetchFilterOptions = isNil(config === null || config === void 0 ? void 0 : config.filterColumns);
  var _useFetchFilterOption = useFetchFilterOptions({
      params: {
        emailSearchTerm: debouncedEmailSearchTerm
      },
      options: {
        enabled: shouldFetchFilterOptions
      }
    }),
    _useFetchFilterOption2 = _useFetchFilterOption.data,
    filterOptions = _useFetchFilterOption2 === void 0 ? {} : _useFetchFilterOption2,
    isFilterOptionsLoading = _useFetchFilterOption.isLoading;
  var filterColumns = shouldFetchFilterOptions ? buildFilterColumns({
    options: filterOptions,
    emailConfig: {
      setSearchTerm: setEmailSearchTerm,
      isLoading: isFilterOptionsLoading
    }
  }) : config === null || config === void 0 ? void 0 : config.filterColumns;
  var searchProps = useMemo(function () {
    return {
      "data-testid": "ntm-search-members-input",
      placeholder: t("neetoTeamMembers.common.search_", {
        what: taxonomies.member.plural
      }),
      onChange: resetPage
    };
  }, []);
  return {
    filters: filters,
    setFilters: setFilters,
    filterColumns: filterColumns,
    searchProps: searchProps,
    searchKeywordProps: searchKeywordProps
  };
};

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), true).forEach(function (r) { _defineProperty(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 useTeams = function useTeams(_ref) {
  var config = _ref.config,
    filters = _ref.filters,
    setFilters = _ref.setFilters,
    updateMember = _ref.updateMember;
  var history = useHistory();
  var _useState = useState(null),
    _useState2 = _slicedToArray(_useState, 2),
    selectedMember = _useState2[0],
    setSelectedMember = _useState2[1];
  var _useState3 = useState(DEFAULT_IS_OPEN_VALUES),
    _useState4 = _slicedToArray(_useState3, 2),
    isOpen = _useState4[0],
    setIsOpen = _useState4[1];
  var _useState5 = useState(DEFAULT_SELECTED_ROWS),
    _useState6 = _slicedToArray(_useState5, 2),
    selectedRows = _useState6[0],
    setSelectedRows = _useState6[1];
  var handleRemoveMember = function handleRemoveMember() {
    if (!selectedMember) return;
    var id = selectedMember.id;
    var payload = {
      active: false
    };
    updateMember({
      id: id,
      payload: payload
    });
  };
  var handleUpdateStatus = function handleUpdateStatus(user) {
    setSelectedMember(user);
    setIsOpen(assoc("alert", true));
  };
  var handleUpdateRole = function handleUpdateRole(user) {
    setSelectedMember(user);
    setIsOpen(assoc("manageMember", true));
  };
  var handleAlertClose = function handleAlertClose() {
    setIsOpen(assoc("alert", false));
    setSelectedMember(null);
  };
  var handlePaneClose = function handlePaneClose() {
    setIsOpen(assoc("manageMember", false));
    setSelectedMember(null);
  };
  var resetSelectedRows = function resetSelectedRows() {
    return setSelectedRows(DEFAULT_SELECTED_ROWS);
  };
  var handleMemberFilterChange = function handleMemberFilterChange() {
    var _filters$pageSize;
    setSelectedRows(DEFAULT_SELECTED_ROWS);
    handlePageChange(setFilters, history)(DEFAULT_PAGE_INDEX, (_filters$pageSize = filters.pageSize) !== null && _filters$pageSize !== void 0 ? _filters$pageSize : DEFAULT_PAGE_SIZE);
  };
  var handleTableChange = function handleTableChange(_, __, sorter) {
    setFilters(mergeLeft({
      sortBy: sorter.field,
      orderBy: sorter.order
    }));
  };
  var handleRowSelect = function handleRowSelect(keys, rowData) {
    var _config$table, _config$table$onRowSe;
    var rows = {
      active: pluck("active", rowData),
      displayNames: pluck("displayName", rowData),
      emails: pluck("email", rowData),
      keys: keys
    };
    setSelectedRows(_objectSpread({}, rows));
    config === null || config === void 0 || (_config$table = config.table) === null || _config$table === void 0 || (_config$table$onRowSe = _config$table.onRowSelect) === null || _config$table$onRowSe === void 0 || _config$table$onRowSe.call(_config$table, {
      rows: rows,
      resetSelectedRows: resetSelectedRows,
      rowData: rowData
    });
  };
  return {
    handleAlertClose: handleAlertClose,
    handleMemberFilterChange: handleMemberFilterChange,
    handlePaneClose: handlePaneClose,
    handleRowSelect: handleRowSelect,
    handleTableChange: handleTableChange,
    handleRemoveMember: handleRemoveMember,
    handleUpdateRole: handleUpdateRole,
    handleUpdateStatus: handleUpdateStatus,
    isOpen: isOpen,
    selectedMember: selectedMember,
    selectedRows: selectedRows,
    setIsOpen: setIsOpen,
    setSelectedRows: setSelectedRows
  };
};

export { useTeams as a, useFilters as u };
//# sourceMappingURL=useTeams-Bw4eltN1.js.map
