'use strict';
import { Server } from 'socket.io';
import { createError } from '../../../../../../fd-toolbox/errors/errors.js';
import { webSocketMethods } from '../../../../../fd-app-toolbox/constants/web-socket-methods.js';
import { httpMethods } from '../../../../../../base-repo/constants/http-methods.js';
import { logInfo } from '../../../../../../fd-toolbox/server/logging/logger.js';
import { webSockets } from './web-sockets.js';
import { httpStatusCodes } from '../../../../../../fd-toolbox/constants/http-status-codes.js';
import { isWebSocketDisabled } from '../../../../../../fd-toolbox/lib/environments.js';

var __defProp=Object['defineProperty'],__name=(_0x13ef91,_0x58ee3d)=>__defProp(_0x13ef91,'name',{'value':_0x58ee3d,'configurable':!![]});function initSocket(_0x15cd1f){!isWebSocketDisabled()&&(io=new Server(_0x15cd1f),io['on'](webSocketMethods['serverConnection'],_0x2681cc=>{logInfo(webSockets['clientConnectedMessage']),socketIdToUserId['set'](_0x2681cc['id'],void(-0x31d+-0x247a+0x2797*0x1)),_0x2681cc['on'](webSocketMethods['authorize'],_0xc730ab=>{socketIdToUserId['set'](_0x2681cc['id'],_0xc730ab);}),_0x2681cc['on'](webSocketMethods['disconnect'],()=>{socketIdToUserId['delete'](_0x2681cc['id']),logInfo(webSockets['clientDisconnectedMessage']);});}));}__name(initSocket,'initSocket');function isSocketInitialized(){return !!io;}__name(isSocketInitialized,'isSocketInitialized');function sendMessage(_0x445c12,_0x2648ae,_0x335b98){let _0x39a321;checkSocketIo();if(!_0x2648ae)_0x39a321=io['emit'](_0x445c12,_0x335b98);else {const _0x245dce=getSocketId(_0x2648ae);if(!_0x245dce)return _0x39a321;else _0x39a321=io['to'](_0x245dce)['emit'](_0x445c12,_0x335b98);}return _0x39a321;}__name(sendMessage,'sendMessage');function checkAndHandleWebSocketRequest(_0x2d22b3,_0x440499){if(_0x2d22b3['method']!==httpMethods['post']||!_0x2d22b3['url'])return;const [_0xe82bcc,_0x48b39e]=_0x2d22b3['url']['split']('?');if(!_0xe82bcc['startsWith'](webSockets['sendUrl']))return;const _0x4e839e=new URLSearchParams(_0x48b39e),_0x48106b=_0x4e839e['get'](webSockets['methodParam']),_0x54c8db=_0x4e839e['get'](webSockets['userIdParam'])??void(-0xd81+0x1118+0x397*-0x1);if(!_0x48106b){_0x440499['statusCode']=httpStatusCodes['badRequest'],_0x440499['end']({'error':webSockets['methodIsRequired']});return;}let _0xeb7281='';_0x2d22b3['on'](webSockets['bodyChunkEvent'],_0x61cfe1=>{_0xeb7281+=_0x61cfe1['toString']();}),_0x2d22b3['on'](webSockets['requestEndEvent'],()=>{sendMessage(_0x48106b,_0x54c8db,JSON['parse'](_0xeb7281)),_0x440499['end'](webSockets['notificationSent']);});}__name(checkAndHandleWebSocketRequest,'checkAndHandleWebSocketRequest');let io;const socketIdToUserId=new Map();function getSocketId(_0x33062d){for(const [_0xa27466,_0x56d174]of socketIdToUserId){if(_0x56d174===_0x33062d)return _0xa27466;}return void(-0x1*-0x258a+0x379*-0x8+-0x9c2);}__name(getSocketId,'getSocketId');function checkSocketIo(){if(!isSocketInitialized())throw createError(webSockets['socketNotInitialized']);}__name(checkSocketIo,'checkSocketIo');

export { checkAndHandleWebSocketRequest, initSocket, isSocketInitialized, sendMessage };
