'use strict';

var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var reactQuery = require('@tanstack/react-query');
var reactUtils = require('@bigbinary/neeto-commons-frontend/react-utils');
var ramda = require('ramda');
var axios = require('axios');
var constants = require('./constants-D_H6M1Pm.js');

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

var _toConsumableArray__default = /*#__PURE__*/_interopDefault(_toConsumableArray);
var axios__default = /*#__PURE__*/_interopDefault(axios);

var list$1 = function list(params) {
  return axios__default.default.get("".concat(constants.BASE_URL, "/api/v1/groups"), {
    params: params
  });
};
var show = function show(id) {
  return axios__default.default.get("".concat(constants.BASE_URL, "/api/v1/groups/").concat(id));
};
var create = function create(payload) {
  return axios__default.default.post("".concat(constants.BASE_URL, "/api/v1/groups"), payload);
};
var update = function update(id, payload) {
  return axios__default.default.patch("".concat(constants.BASE_URL, "/api/v1/groups/").concat(id), payload);
};
var destroy = function destroy(id) {
  return axios__default.default["delete"]("".concat(constants.BASE_URL, "/api/v1/groups/").concat(id));
};
var groupsApi = {
  create: create,
  list: list$1,
  update: update,
  show: show,
  destroy: destroy
};

var useFetchGroups = function useFetchGroups(_ref) {
  var page = _ref.page,
    pageSize = _ref.pageSize,
    _ref$searchTerm = _ref.searchTerm,
    searchTerm = _ref$searchTerm === void 0 ? "" : _ref$searchTerm;
  return reactQuery.useQuery({
    queryKey: [constants.QUERY_KEYS.GROUPS, searchTerm, page, pageSize],
    queryFn: function queryFn() {
      return groupsApi.list({
        searchTerm: searchTerm,
        page: page,
        pageSize: pageSize,
        loadDetails: true
      });
    }
  });
};
var useCreateGroup = function useCreateGroup() {
  var _config$form$otherKey, _config$form;
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactUtils.useMutationWithInvalidation(function (payload) {
    return groupsApi.create(payload);
  }, {
    keysToInvalidate: [[constants.QUERY_KEYS.GROUPS]].concat(_toConsumableArray__default.default((_config$form$otherKey = config === null || config === void 0 || (_config$form = config.form) === null || _config$form === void 0 ? void 0 : _config$form.otherKeysToInvalidate) !== null && _config$form$otherKey !== void 0 ? _config$form$otherKey : []))
  });
};
var useDeleteGroup = function useDeleteGroup() {
  var _config$form$otherKey2, _config$form2;
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactUtils.useMutationWithInvalidation(function (id) {
    return groupsApi.destroy(id);
  }, {
    keysToInvalidate: [[constants.QUERY_KEYS.GROUPS]].concat(_toConsumableArray__default.default((_config$form$otherKey2 = config === null || config === void 0 || (_config$form2 = config.form) === null || _config$form2 === void 0 ? void 0 : _config$form2.otherKeysToInvalidate) !== null && _config$form$otherKey2 !== void 0 ? _config$form$otherKey2 : []))
  });
};
var useFetchGroup = function useFetchGroup(id) {
  return reactQuery.useQuery({
    queryKey: [constants.QUERY_KEYS.GROUPS, id],
    queryFn: function queryFn() {
      return groupsApi.show(id);
    },
    select: ramda.prop("group"),
    enabled: !!id
  });
};
var useUpdateGroup = function useUpdateGroup() {
  var _config$form$otherKey3, _config$form3;
  var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactUtils.useMutationWithInvalidation(function (_ref2) {
    var id = _ref2.id,
      payload = _ref2.payload;
    return groupsApi.update(id, payload);
  }, {
    keysToInvalidate: [[constants.QUERY_KEYS.GROUPS]].concat(_toConsumableArray__default.default((_config$form$otherKey3 = config === null || config === void 0 || (_config$form3 = config.form) === null || _config$form3 === void 0 ? void 0 : _config$form3.otherKeysToInvalidate) !== null && _config$form$otherKey3 !== void 0 ? _config$form$otherKey3 : []))
  });
};

var list = function list() {
  return axios__default.default.get("".concat(constants.BASE_URL, "/api/v1/users"));
};
var usersApi = {
  list: list
};

var useFetchUsers = function useFetchUsers() {
  return reactQuery.useQuery({
    queryKey: [constants.QUERY_KEYS.USERS],
    queryFn: usersApi.list
  });
};

exports.useCreateGroup = useCreateGroup;
exports.useDeleteGroup = useDeleteGroup;
exports.useFetchGroup = useFetchGroup;
exports.useFetchGroups = useFetchGroups;
exports.useFetchUsers = useFetchUsers;
exports.useUpdateGroup = useUpdateGroup;
//# sourceMappingURL=useUsersApi-CBZ1Dk5d.js.map
