import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import { useQuery, keepPreviousData, useMutation } from '@tanstack/react-query';
import { globalProps } from '@bigbinary/neeto-commons-frontend/initializers';
import { useMutationWithInvalidation } from '@bigbinary/neeto-commons-frontend/react-utils';
import { identity } from 'ramda';
import axios from 'axios';
import { keysToSnakeCase } from '@bigbinary/neeto-cist';
import qs from 'qs';
import { B as BASE_URL, Q as QUERY_KEYS } from './constants-CXdiW8NZ.js';
import { q as queryClient } from './queryClient-Dsu-gu4Y.js';

var fetch = function fetch(params) {
  var serializedParams = qs.stringify(keysToSnakeCase(params), {
    arrayFormat: "brackets",
    encode: true
  });
  return axios.get("".concat(BASE_URL, "/teams?").concat(serializedParams));
};
var fetchCountsPerCategory = function fetchCountsPerCategory() {
  return axios.get("".concat(BASE_URL, "/teams/counts_per_category"));
};
var create = function create(payload) {
  return axios.post("".concat(BASE_URL, "/teams"), payload);
};
var update = function update(id, payload) {
  return axios.put("".concat(BASE_URL, "/teams/").concat(id), {
    team: payload
  });
};
var bulkUpdate = function bulkUpdate(payload) {
  return axios.patch("".concat(BASE_URL, "/teams/bulk_update"), payload);
};
var fetchFilterOptions = function fetchFilterOptions(params) {
  return axios.get("".concat(BASE_URL, "/teams/filter_options"), {
    params: keysToSnakeCase(params)
  });
};
var creationStatus = function creationStatus(jobId) {
  return axios.get("".concat(BASE_URL, "/teams/creation_status/").concat(jobId));
};
var show = function show(id) {
  return axios.get("".concat(BASE_URL, "/teams/").concat(id));
};
var exportMembers = function exportMembers(payload) {
  return axios.post("".concat(BASE_URL, "/teams/export"), payload, {
    showToastr: false
  });
};
var teamsApi = {
  bulkUpdate: bulkUpdate,
  create: create,
  fetch: fetch,
  show: show,
  update: update,
  fetchFilterOptions: fetchFilterOptions,
  fetchCountsPerCategory: fetchCountsPerCategory,
  creationStatus: creationStatus,
  "export": exportMembers
};

var _excluded = ["onSuccess"],
  _excluded2 = ["onSuccess"];
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 useFetchMembers = function useFetchMembers() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return useQuery(_objectSpread(_objectSpread({
    queryKey: [QUERY_KEYS.TEAMS, params],
    queryFn: function queryFn() {
      return teamsApi.fetch(params);
    },
    select: identity,
    placeholderData: keepPreviousData
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useFetchMembersCountsPerCategory = function useFetchMembersCountsPerCategory() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return useQuery(_objectSpread(_objectSpread({
    queryKey: [QUERY_KEYS.TEAMS],
    queryFn: teamsApi.fetchCountsPerCategory,
    select: identity,
    placeholderData: keepPreviousData
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useCreateMember = function useCreateMember() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return useMutation(_objectSpread({
    mutationFn: teamsApi.create
  }, options));
};
var useExportMembers = function useExportMembers(options) {
  return useMutation(_objectSpread({
    mutationFn: teamsApi["export"]
  }, options));
};
var useUpdateMember = function useUpdateMember(_ref) {
  var _onSuccess = _ref.onSuccess,
    options = _objectWithoutProperties(_ref, _excluded);
  return useMutationWithInvalidation(function (_ref2) {
    var id = _ref2.id,
      payload = _ref2.payload;
    return teamsApi.update(id, payload);
  }, _objectSpread({
    keysToInvalidate: [[QUERY_KEYS.TEAMS], [QUERY_KEYS.TEAMS_DEPENDENCIES], [QUERY_KEYS.TEAM_MEMBER_DETAIL]],
    onSuccess: function onSuccess(_, params) {
      var _globalProps$user;
      _onSuccess === null || _onSuccess === void 0 || _onSuccess();
      if (params.payload.active === false && ((_globalProps$user = globalProps.user) === null || _globalProps$user === void 0 ? void 0 : _globalProps$user.id) === params.id) {
        window.location.pathname = "/admin";
      }
    }
  }, options));
};
var useBulkUpdateMembers = function useBulkUpdateMembers(_ref3) {
  var _onSuccess2 = _ref3.onSuccess,
    options = _objectWithoutProperties(_ref3, _excluded2);
  return useMutationWithInvalidation(teamsApi.bulkUpdate, _objectSpread({
    keysToInvalidate: [[QUERY_KEYS.TEAMS], [QUERY_KEYS.TEAMS_DEPENDENCIES]],
    onSuccess: function onSuccess(_, params) {
      var _params$users, _params$users2, _globalProps$user2;
      _onSuccess2 === null || _onSuccess2 === void 0 || _onSuccess2();
      if (((_params$users = params.users) === null || _params$users === void 0 ? void 0 : _params$users.active) === false && (_params$users2 = params.users) !== null && _params$users2 !== void 0 && (_params$users2 = _params$users2.emails) !== null && _params$users2 !== void 0 && _params$users2.includes((_globalProps$user2 = globalProps.user) === null || _globalProps$user2 === void 0 ? void 0 : _globalProps$user2.email)) {
        window.location.pathname = "/admin";
      }
    }
  }, options));
};
var refetchMembers = function refetchMembers() {
  queryClient.invalidateQueries({
    queryKey: [QUERY_KEYS.TEAMS]
  });
  queryClient.invalidateQueries({
    queryKey: [QUERY_KEYS.TEAMS_DEPENDENCIES]
  });
};
var useFetchFilterOptions = function useFetchFilterOptions(_ref4) {
  var _ref4$params = _ref4.params,
    params = _ref4$params === void 0 ? {} : _ref4$params,
    _ref4$options = _ref4.options,
    options = _ref4$options === void 0 ? {} : _ref4$options;
  return useQuery(_objectSpread(_objectSpread({
    queryKey: [QUERY_KEYS.TEAMS_DEPENDENCIES, params],
    queryFn: function queryFn() {
      return teamsApi.fetchFilterOptions(params);
    },
    select: identity
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useFetchMember = function useFetchMember(id) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return useQuery(_objectSpread(_objectSpread({
    queryKey: [QUERY_KEYS.TEAM_MEMBER_DETAIL, id],
    queryFn: function queryFn() {
      return teamsApi.show(id);
    }
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useMembersCreationStatus = function useMembersCreationStatus(jobId, options) {
  return useQuery(_objectSpread({
    queryKey: [QUERY_KEYS.CREATION_STATUS, jobId],
    queryFn: function queryFn() {
      return teamsApi.creationStatus(jobId);
    }
  }, options));
};

export { useFetchMembersCountsPerCategory as a, useFetchFilterOptions as b, useFetchMember as c, useCreateMember as d, useMembersCreationStatus as e, useUpdateMember as f, useBulkUpdateMembers as g, useExportMembers as h, refetchMembers as r, useFetchMembers as u };
//# sourceMappingURL=useTeamsApi-DkgWqw-g.js.map
