'use strict';

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var React = require('react');
var constants = require('@bigbinary/neeto-commons-frontend/constants');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');
var reactUtils = require('@bigbinary/neeto-commons-frontend/react-utils');
var utils = require('@bigbinary/neeto-filters-frontend/utils');
var ramda = require('ramda');
var reactI18next = require('react-i18next');
var useTeamsApi = require('./useTeamsApi-ClWGPxNW.js');
var utils$1 = require('./utils-CgwJI9pk.js');
var reactRouterDom = require('react-router-dom');

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

var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefault(_slicedToArray);

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__default.default(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 = reactI18next.useTranslation(),
    t = _useTranslation.t;
  var _useQueryParams = reactUtils.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 = React.useState(""),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    emailSearchTerm = _useState2[0],
    setEmailSearchTerm = _useState2[1];
  var defaultNeetoFilters = utils.buildFiltersFromURL([].concat(_toConsumableArray__default.default((_config$filterColumns = config === null || config === void 0 ? void 0 : config.filterColumns) !== null && _config$filterColumns !== void 0 ? _config$filterColumns : utils$1.defaultNeetoFiltersColumns()), [searchKeywordProps]));
  var _useState3 = React.useState(_objectSpread$1(_objectSpread$1(_objectSpread$1({}, utils$1.DEFAULT_FILTER_VALUES), config === null || config === void 0 ? void 0 : config.defaultFilterValues), {}, {
      neetoFilters: defaultNeetoFilters
    })),
    _useState4 = _slicedToArray__default.default(_useState3, 2),
    filters = _useState4[0],
    setFilters = _useState4[1];
  var resetPage = reactUtils.useFuncDebounce(function () {
    return setFilters(ramda.mergeLeft({
      page: constants.DEFAULT_PAGE_INDEX
    }));
  });
  var debouncedEmailSearchTerm = reactUtils.useDebounce(emailSearchTerm, 750);
  var shouldFetchFilterOptions = ramda.isNil(config === null || config === void 0 ? void 0 : config.filterColumns);
  var _useFetchFilterOption = useTeamsApi.useFetchFilterOptions({
      params: {
        emailSearchTerm: debouncedEmailSearchTerm
      },
      options: {
        enabled: shouldFetchFilterOptions
      }
    }),
    _useFetchFilterOption2 = _useFetchFilterOption.data,
    filterOptions = _useFetchFilterOption2 === void 0 ? {} : _useFetchFilterOption2,
    isFilterOptionsLoading = _useFetchFilterOption.isLoading;
  var filterColumns = shouldFetchFilterOptions ? utils$1.buildFilterColumns({
    options: filterOptions,
    emailConfig: {
      setSearchTerm: setEmailSearchTerm,
      isLoading: isFilterOptionsLoading
    }
  }) : config === null || config === void 0 ? void 0 : config.filterColumns;
  var searchProps = React.useMemo(function () {
    return {
      "data-testid": "ntm-search-members-input",
      placeholder: t("neetoTeamMembers.common.search_", {
        what: initializers.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__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 useTeams = function useTeams(_ref) {
  var config = _ref.config,
    filters = _ref.filters,
    setFilters = _ref.setFilters,
    updateMember = _ref.updateMember;
  var history = reactRouterDom.useHistory();
  var _useState = React.useState(null),
    _useState2 = _slicedToArray__default.default(_useState, 2),
    selectedMember = _useState2[0],
    setSelectedMember = _useState2[1];
  var _useState3 = React.useState(utils$1.DEFAULT_IS_OPEN_VALUES),
    _useState4 = _slicedToArray__default.default(_useState3, 2),
    isOpen = _useState4[0],
    setIsOpen = _useState4[1];
  var _useState5 = React.useState(utils$1.DEFAULT_SELECTED_ROWS),
    _useState6 = _slicedToArray__default.default(_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(ramda.assoc("alert", true));
  };
  var handleUpdateRole = function handleUpdateRole(user) {
    setSelectedMember(user);
    setIsOpen(ramda.assoc("manageMember", true));
  };
  var handleAlertClose = function handleAlertClose() {
    setIsOpen(ramda.assoc("alert", false));
    setSelectedMember(null);
  };
  var handlePaneClose = function handlePaneClose() {
    setIsOpen(ramda.assoc("manageMember", false));
    setSelectedMember(null);
  };
  var resetSelectedRows = function resetSelectedRows() {
    return setSelectedRows(utils$1.DEFAULT_SELECTED_ROWS);
  };
  var handleMemberFilterChange = function handleMemberFilterChange() {
    var _filters$pageSize;
    setSelectedRows(utils$1.DEFAULT_SELECTED_ROWS);
    utils$1.handlePageChange(setFilters, history)(constants.DEFAULT_PAGE_INDEX, (_filters$pageSize = filters.pageSize) !== null && _filters$pageSize !== void 0 ? _filters$pageSize : constants.DEFAULT_PAGE_SIZE);
  };
  var handleTableChange = function handleTableChange(_, __, sorter) {
    setFilters(ramda.mergeLeft({
      sortBy: sorter.field,
      orderBy: sorter.order
    }));
  };
  var handleRowSelect = function handleRowSelect(keys, rowData) {
    var _config$table, _config$table$onRowSe;
    var rows = {
      active: ramda.pluck("active", rowData),
      displayNames: ramda.pluck("displayName", rowData),
      emails: ramda.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
  };
};

exports.useFilters = useFilters;
exports.useTeams = useTeams;
//# sourceMappingURL=useTeams-C9N2TJY-.js.map
