'use strict';

var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var reactQuery = require('@tanstack/react-query');
var initializers = require('@bigbinary/neeto-commons-frontend/initializers');
var reactUtils = require('@bigbinary/neeto-commons-frontend/react-utils');
var ramda = require('ramda');
var axios = require('axios');
var neetoCist = require('@bigbinary/neeto-cist');
var qs = require('qs');
var constants = require('./constants-D_H6M1Pm.js');
var queryClient = require('./queryClient-4vAFE3bx.js');

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

var _objectWithoutProperties__default = /*#__PURE__*/_interopDefault(_objectWithoutProperties);
var _defineProperty__default = /*#__PURE__*/_interopDefault(_defineProperty);
var axios__default = /*#__PURE__*/_interopDefault(axios);
var qs__default = /*#__PURE__*/_interopDefault(qs);

var fetch = function fetch(params) {
  var serializedParams = qs__default.default.stringify(neetoCist.keysToSnakeCase(params), {
    arrayFormat: "brackets",
    encode: true
  });
  return axios__default.default.get("".concat(constants.BASE_URL, "/teams?").concat(serializedParams));
};
var fetchCountsPerCategory = function fetchCountsPerCategory() {
  return axios__default.default.get("".concat(constants.BASE_URL, "/teams/counts_per_category"));
};
var create = function create(payload) {
  return axios__default.default.post("".concat(constants.BASE_URL, "/teams"), payload);
};
var update = function update(id, payload) {
  return axios__default.default.put("".concat(constants.BASE_URL, "/teams/").concat(id), {
    team: payload
  });
};
var bulkUpdate = function bulkUpdate(payload) {
  return axios__default.default.patch("".concat(constants.BASE_URL, "/teams/bulk_update"), payload);
};
var fetchFilterOptions = function fetchFilterOptions(params) {
  return axios__default.default.get("".concat(constants.BASE_URL, "/teams/filter_options"), {
    params: neetoCist.keysToSnakeCase(params)
  });
};
var creationStatus = function creationStatus(jobId) {
  return axios__default.default.get("".concat(constants.BASE_URL, "/teams/creation_status/").concat(jobId));
};
var show = function show(id) {
  return axios__default.default.get("".concat(constants.BASE_URL, "/teams/").concat(id));
};
var exportMembers = function exportMembers(payload) {
  return axios__default.default.post("".concat(constants.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__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 useFetchMembers = function useFetchMembers() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return reactQuery.useQuery(_objectSpread(_objectSpread({
    queryKey: [constants.QUERY_KEYS.TEAMS, params],
    queryFn: function queryFn() {
      return teamsApi.fetch(params);
    },
    select: ramda.identity,
    placeholderData: reactQuery.keepPreviousData
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useFetchMembersCountsPerCategory = function useFetchMembersCountsPerCategory() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactQuery.useQuery(_objectSpread(_objectSpread({
    queryKey: [constants.QUERY_KEYS.TEAMS],
    queryFn: teamsApi.fetchCountsPerCategory,
    select: ramda.identity,
    placeholderData: reactQuery.keepPreviousData
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useCreateMember = function useCreateMember() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactQuery.useMutation(_objectSpread({
    mutationFn: teamsApi.create
  }, options));
};
var useExportMembers = function useExportMembers(options) {
  return reactQuery.useMutation(_objectSpread({
    mutationFn: teamsApi["export"]
  }, options));
};
var useUpdateMember = function useUpdateMember(_ref) {
  var _onSuccess = _ref.onSuccess,
    options = _objectWithoutProperties__default.default(_ref, _excluded);
  return reactUtils.useMutationWithInvalidation(function (_ref2) {
    var id = _ref2.id,
      payload = _ref2.payload;
    return teamsApi.update(id, payload);
  }, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS], [constants.QUERY_KEYS.TEAMS_DEPENDENCIES], [constants.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 = initializers.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__default.default(_ref3, _excluded2);
  return reactUtils.useMutationWithInvalidation(teamsApi.bulkUpdate, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS], [constants.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 = initializers.globalProps.user) === null || _globalProps$user2 === void 0 ? void 0 : _globalProps$user2.email)) {
        window.location.pathname = "/admin";
      }
    }
  }, options));
};
var refetchMembers = function refetchMembers() {
  queryClient.queryClient.invalidateQueries({
    queryKey: [constants.QUERY_KEYS.TEAMS]
  });
  queryClient.queryClient.invalidateQueries({
    queryKey: [constants.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 reactQuery.useQuery(_objectSpread(_objectSpread({
    queryKey: [constants.QUERY_KEYS.TEAMS_DEPENDENCIES, params],
    queryFn: function queryFn() {
      return teamsApi.fetchFilterOptions(params);
    },
    select: ramda.identity
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useFetchMember = function useFetchMember(id) {
  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  return reactQuery.useQuery(_objectSpread(_objectSpread({
    queryKey: [constants.QUERY_KEYS.TEAM_MEMBER_DETAIL, id],
    queryFn: function queryFn() {
      return teamsApi.show(id);
    }
  }, options), {}, {
    refetchOnWindowFocus: false
  }));
};
var useMembersCreationStatus = function useMembersCreationStatus(jobId, options) {
  return reactQuery.useQuery(_objectSpread({
    queryKey: [constants.QUERY_KEYS.CREATION_STATUS, jobId],
    queryFn: function queryFn() {
      return teamsApi.creationStatus(jobId);
    }
  }, options));
};

exports.refetchMembers = refetchMembers;
exports.useBulkUpdateMembers = useBulkUpdateMembers;
exports.useCreateMember = useCreateMember;
exports.useExportMembers = useExportMembers;
exports.useFetchFilterOptions = useFetchFilterOptions;
exports.useFetchMember = useFetchMember;
exports.useFetchMembers = useFetchMembers;
exports.useFetchMembersCountsPerCategory = useFetchMembersCountsPerCategory;
exports.useMembersCreationStatus = useMembersCreationStatus;
exports.useUpdateMember = useUpdateMember;
//# sourceMappingURL=useTeamsApi-ClWGPxNW.js.map
