'use strict';

var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var reactQuery = require('@tanstack/react-query');
var neetoCist = require('@bigbinary/neeto-cist');
var reactUtils = require('@bigbinary/neeto-commons-frontend/react-utils');
var axios = require('axios');
var constants = require('./constants-D_H6M1Pm.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 fetch$1 = function fetch() {
  return axios__default.default.get("".concat(constants.BASE_URL, "/global_roles"));
};
var globalRolesApi = {
  fetch: fetch$1
};

var fetch = function fetch() {
  return axios__default.default.get("".concat(constants.BASE_URL, "/organization_roles"));
};
var create = function create(payload) {
  return axios__default.default.post("".concat(constants.BASE_URL, "/organization_roles"), payload);
};
var update = function update(id, payload) {
  return axios__default.default.patch("".concat(constants.BASE_URL, "/organization_roles/").concat(id), payload);
};
var destroy = function destroy(id, payload) {
  return axios__default.default["delete"]("".concat(constants.BASE_URL, "/organization_roles/").concat(id), {
    data: payload
  });
};
var bulkUpdate = function bulkUpdate(payload) {
  return axios__default.default.patch("".concat(constants.BASE_URL, "/organization_roles/bulk_update"), payload);
};
var organizationRolesApi = {
  bulkUpdate: bulkUpdate,
  create: create,
  destroy: destroy,
  fetch: fetch,
  update: update
};

var _excluded = ["onSuccess"],
  _excluded2 = ["onSuccess"],
  _excluded3 = ["onSuccess"],
  _excluded4 = ["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 useFetchRoles = function useFetchRoles() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactQuery.useQuery(_objectSpread({
    queryKey: [constants.QUERY_KEYS.ROLES],
    queryFn: organizationRolesApi.fetch,
    select: function select(response) {
      return response === null || response === void 0 ? void 0 : response.organizationRoles;
    }
  }, options));
};
var useFetchGlobalRoles = function useFetchGlobalRoles() {
  var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return reactQuery.useQuery(_objectSpread({
    queryKey: [constants.QUERY_KEYS.GLOBAL_ROLES],
    queryFn: globalRolesApi.fetch
  }, options));
};
var useCreateRole = function useCreateRole(_ref) {
  var _onSuccess = _ref.onSuccess,
    options = _objectWithoutProperties__default.default(_ref, _excluded);
  return reactUtils.useMutationWithInvalidation(organizationRolesApi.create, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS_DEPENDENCIES], [constants.QUERY_KEYS.ROLES]],
    onSuccess: function onSuccess() {
      _onSuccess === null || _onSuccess === void 0 || _onSuccess();
    }
  }, options));
};
var useUpdateRole = function useUpdateRole(_ref2) {
  var _onSuccess2 = _ref2.onSuccess,
    options = _objectWithoutProperties__default.default(_ref2, _excluded2);
  return reactUtils.useMutationWithInvalidation(function (_ref3) {
    var id = _ref3.id,
      payload = _ref3.payload;
    return organizationRolesApi.update(id, payload);
  }, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS], [constants.QUERY_KEYS.TEAMS_DEPENDENCIES], [constants.QUERY_KEYS.ROLES]],
    onSuccess: function onSuccess() {
      _onSuccess2 === null || _onSuccess2 === void 0 || _onSuccess2();
    }
  }, options));
};
var useDestroyRole = function useDestroyRole(_ref4) {
  var _ref4$onSuccess = _ref4.onSuccess,
    _onSuccess3 = _ref4$onSuccess === void 0 ? neetoCist.noop : _ref4$onSuccess,
    options = _objectWithoutProperties__default.default(_ref4, _excluded3);
  return reactUtils.useMutationWithInvalidation(function (_ref5) {
    var id = _ref5.id,
      payload = _ref5.payload;
    return organizationRolesApi.destroy(id, payload);
  }, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS], [constants.QUERY_KEYS.TEAMS_DEPENDENCIES], [constants.QUERY_KEYS.ROLES]],
    onSuccess: function onSuccess() {
      _onSuccess3 === null || _onSuccess3 === void 0 || _onSuccess3();
    }
  }, options));
};
var useBulkUpdateRoles = function useBulkUpdateRoles(_ref6) {
  var _onSuccess4 = _ref6.onSuccess,
    options = _objectWithoutProperties__default.default(_ref6, _excluded4);
  return reactUtils.useMutationWithInvalidation(organizationRolesApi.bulkUpdate, _objectSpread({
    keysToInvalidate: [[constants.QUERY_KEYS.TEAMS], [constants.QUERY_KEYS.TEAMS_DEPENDENCIES], [constants.QUERY_KEYS.ROLES]],
    onSuccess: function onSuccess() {
      _onSuccess4 === null || _onSuccess4 === void 0 || _onSuccess4();
    }
  }, options));
};

exports.useBulkUpdateRoles = useBulkUpdateRoles;
exports.useCreateRole = useCreateRole;
exports.useDestroyRole = useDestroyRole;
exports.useFetchGlobalRoles = useFetchGlobalRoles;
exports.useFetchRoles = useFetchRoles;
exports.useUpdateRole = useUpdateRole;
//# sourceMappingURL=useRolesApi-DuL4eCuF.js.map
