'use strict';
import { logAndNotify } from '../errors/error-handler.js';
import { httpMethods } from '../../base-repo/constants/http-methods.js';
import { getProblemDetails } from '../errors/problem-details.js';
import { httpStatusCodes } from '../constants/http-status-codes.js';
import { goToLogin, getRelativeLoginUrl } from '../routing/login-routers.js';
import { removeLoginState } from '../auth/internal-login-states.js';
import { isBrowser } from '../lib/environments.js';
import { getJsonFromResponse } from '../lib/utils.js';
import { httpContentTypes } from '../constants/header-names.js';

var __defProp=Object['defineProperty'],__name=(_0x9886a4,_0x587b43)=>__defProp(_0x9886a4,'name',{'value':_0x587b43,'configurable':!![]});async function apiBaseGetResponse(_0x5288ec,_0x2a5932){const _0xa05424=await fetch(_0x5288ec,_0x2a5932);return isUnauthorizedError(_0xa05424['status'])&&(removeLoginState(),goToLogin()),_0xa05424;}__name(apiBaseGetResponse,'apiBaseGetResponse');async function apiBaseGet(_0x5c1c7a,_0x536308){const _0x1c6317=await fetch(_0x5c1c7a,{'method':httpMethods['get'],'headers':_0x536308});return baseApiHandleResponse(_0x1c6317);}__name(apiBaseGet,'apiBaseGet');async function apiBaseGetBlob(_0x33fee3,_0x92d806){const _0x240430=await fetch(_0x33fee3,{'method':httpMethods['get'],'headers':_0x92d806});return baseApiHandleBlobResponse(_0x240430);}__name(apiBaseGetBlob,'apiBaseGetBlob');async function postBaseApi(_0x675f0c,_0xa3332e,_0x42feff){const _0x324a2a=await fetch(_0x675f0c,{'method':httpMethods['post'],'headers':_0x42feff,'body':_0xa3332e instanceof URLSearchParams?_0xa3332e:JSON['stringify'](_0xa3332e)});return baseApiHandleResponse(_0x324a2a);}__name(postBaseApi,'postBaseApi');async function putBaseApi(_0x104689,_0x47c267,_0x3c1b5c){const _0x58c970=await fetch(_0x104689,{'method':httpMethods['put'],'headers':_0x3c1b5c,'body':JSON['stringify'](_0x47c267)});return baseApiHandleResponse(_0x58c970);}__name(putBaseApi,'putBaseApi');async function deleteBaseApi(_0x4b5767,_0x1f68ff){const _0x1c7e71=await fetch(_0x4b5767,{'method':httpMethods['delete'],'headers':_0x1f68ff});if(!_0x1c7e71['ok'])handleErrorResponse(_0x1c7e71);}__name(deleteBaseApi,'deleteBaseApi');async function baseApiHandleResponse(_0xeed274){const _0x1ae16e=_0xeed274['headers']['get'](httpContentTypes['contentTypeHeaderName']),_0x26e9c8=_0x1ae16e?.['includes'](httpContentTypes['contentTypeApplicationJson']);let _0x541335;isUnauthorizedError(_0xeed274['status'])&&(removeLoginState(),goToLogin());if(!_0xeed274['ok'])_0x541335=await handleErrorResponse(_0xeed274);else _0xeed274['status']===httpStatusCodes['noContent']||!_0x26e9c8?_0x541335=void(-0x5*-0x5e5+-0x54f+-0x6*0x407):_0x541335=await getJsonFromResponse(_0xeed274);return _0x541335;}__name(baseApiHandleResponse,'baseApiHandleResponse');async function baseApiHandleBlobResponse(_0x443484){let _0xc02875;isUnauthorizedError(_0x443484['status'])&&goToLogin();if(!_0x443484['ok'])_0xc02875=await handleErrorResponse(_0x443484);else _0x443484['status']===httpStatusCodes['noContent']?_0xc02875=void(0x1296*-0x1+-0x1*0x1dd3+0x3069):_0xc02875=await _0x443484['blob']();return _0xc02875;}__name(baseApiHandleBlobResponse,'baseApiHandleBlobResponse');async function handleErrorResponse(_0x253d0c){const _0x16fa04=_0x253d0c['headers']['get'](httpContentTypes['contentTypeHeaderName']),_0x53be96=_0x16fa04?.['includes'](httpContentTypes['contentTypeApplicationJson']);let _0x10fb2f='',_0x41de8d='';if(_0x53be96){const _0x3e0689=await getJsonFromResponse(_0x253d0c);_0x10fb2f=_0x3e0689?.['message'],_0x41de8d=_0x3e0689?.['description']||'';}_0x10fb2f=_0x10fb2f?_0x10fb2f:_0x253d0c['statusText'];const _0x3b714c={'status':_0x253d0c['status'],'message':_0x10fb2f},_0x1cbe33={'originalUrl':_0x253d0c['url']};return _0x253d0c['status']!==httpStatusCodes['unauthorized']&&logAndNotify(new Error(_0x10fb2f)),getProblemDetails(_0x3b714c,_0x1cbe33,_0x41de8d);}__name(handleErrorResponse,'handleErrorResponse');function isUnauthorizedError(_0x1fbca1){return _0x1fbca1===httpStatusCodes['unauthorized']&&isBrowser()&&window['location']['pathname']!==getRelativeLoginUrl();}__name(isUnauthorizedError,'isUnauthorizedError');

export { apiBaseGet, apiBaseGetBlob, apiBaseGetResponse, baseApiHandleBlobResponse, baseApiHandleResponse, deleteBaseApi, handleErrorResponse, postBaseApi, putBaseApi };
