'use strict';
import { fdIsDynamicServerError, ApiError } from '../../../../fd-toolbox/server/framework/index.js';
import { httpStatusCodes } from '../../../../fd-toolbox/constants/http-status-codes.js';
import { logNextJsMessage, logError } from '../../../../fd-toolbox/server/logging/logger.js';
import { getJsonResponse } from './responses.js';
import { responseMessages } from '../../../../fd-toolbox/server/web/response-messages.js';
import { setCurrentUrl } from './current-url.js';
import { apiAuthorize } from '../auth/auth-guard.js';
import { metaErrorMessages } from '../../constants/meta-error-messages-constants.js';
import { apiForbiddenError } from '../../../../fd-toolbox/server/web/errors.js';
import { isProdOrDemo } from '../../../../fd-toolbox/lib/environments.js';

var __defProp=Object['defineProperty'],__name=(_0x2a45c3,_0x22bab8)=>__defProp(_0x2a45c3,'name',{'value':_0x22bab8,'configurable':!![]});function createHttpApiWrapper(){return __name(function _0x1b1579(_0x126dce,_0x11d515){return async function(_0x22d028,{params:_0x45aa5f}){try{forbidOnProd(_0x11d515?.['forbidOnProd']);const _0x40a266=new URL(_0x22d028['url']);!_0x11d515?.['anonymous']&&await apiAuthorize();setCurrentUrl(_0x40a266);const _0x5dccc0=await _0x45aa5f,_0x35cf38={'params':_0x5dccc0},_0xaa3070=await _0x126dce(_0x22d028,_0x35cf38);return logNextJsMessage(_0x22d028['method'],_0x22d028['url'],_0xaa3070['status']['toString']()),_0xaa3070;}catch(_0x40951a){if(fdIsDynamicServerError(_0x40951a))throw _0x40951a;logError(metaErrorMessages['consoleErrorMessage'],_0x40951a);if(_0x40951a instanceof ApiError){const _0x2c3903=handleApiError(_0x40951a);return logNextJsMessage(_0x22d028['method'],_0x22d028['url'],_0x40951a['statusCode']['toString']()),_0x2c3903;}else return _0x40951a instanceof Error?handleGeneralError(_0x40951a):getJsonResponse({'message':responseMessages['unhandledError']},httpStatusCodes['internalServerError']);}};},'httpApiWrapper');}__name(createHttpApiWrapper,'createHttpApiWrapper');function handleApiError(_0x533e66){let _0xf5eb0c;return isProdOrDemo()?_0xf5eb0c={'message':metaErrorMessages['genericErrorMessage']}:_0xf5eb0c={'message':_0x533e66['message'],'stack':_0x533e66['stack'],'cause':_0x533e66['cause']},getJsonResponse(_0xf5eb0c,_0x533e66['statusCode']);}__name(handleApiError,'handleApiError');function handleGeneralError(_0x46d40f){let _0x5a7d55;return isProdOrDemo()?'userFriendlyMessage'in _0x46d40f?_0x5a7d55={'message':String(_0x46d40f['userFriendlyMessage'])}:_0x5a7d55={'message':metaErrorMessages['genericErrorMessage']}:_0x5a7d55={'message':_0x46d40f['message'],'stack':_0x46d40f['stack'],'cause':_0x46d40f['cause']},getJsonResponse(_0x5a7d55,httpStatusCodes['internalServerError']);}__name(handleGeneralError,'handleGeneralError');function forbidOnProd(_0x183d41){if(isProdOrDemo()&&_0x183d41)throw apiForbiddenError(responseMessages['apiNotAllowedOnProduction']);}__name(forbidOnProd,'forbidOnProd');

export { createHttpApiWrapper, handleApiError, handleGeneralError };
