import { HTTP_METHODS } from '../../common/types';
import log from '../../Logger';
import { METHODS, MOBIUS_SOCKET_MAPPER_FILE } from '../constants';
import { MOBIUS_SOCKET_MESSAGE_TYPE } from './constants';
export function deriveMobiusSocketMessageType(uri, httpMethodType) {
    const logContext = {
        file: MOBIUS_SOCKET_MAPPER_FILE,
        method: METHODS.DERIVE_MOBIUS_SOCKET_MESSAGE_TYPE,
    };
    if (!uri) {
        log.warn('Cannot derive Mobius socket message type: uri is empty', logContext);
        return MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
    }
    if (uri.includes('/services')) {
        if (uri.includes('/callhold/hold')) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_HOLD;
        }
        if (uri.includes('/callhold/resume')) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_RESUME;
        }
        if (uri.includes('/calltransfer/commit')) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_TRANSFER;
        }
        log.warn(`Unrecognized supplementary service uri - uri: ${uri}, httpMethod: ${httpMethodType}`, logContext);
        return MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
    }
    if (uri.includes('/calls/') && uri.includes('/media')) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_MEDIA;
    }
    if (uri.includes('/calls/') && uri.includes('/status')) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_STATUS;
    }
    if (uri.match(/\/calls\/[^/]+$/)) {
        if (httpMethodType === HTTP_METHODS.PATCH) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_STATE;
        }
        if (httpMethodType === HTTP_METHODS.DELETE) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_DELETE;
        }
        log.warn(`Unrecognized httpMethod for calls/{callId} - uri: ${uri}, httpMethod: ${httpMethodType}`, logContext);
        return MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
    }
    if (uri.endsWith('/call')) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.CALL_SETUP;
    }
    if (!uri.includes('/calls/') && uri.endsWith('/status')) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_STATUS;
    }
    if (uri.endsWith('/device')) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.REGISTER;
    }
    if (uri.match(/\/devices\/[^/?]+$/) && !uri.includes('/calls')) {
        if (httpMethodType === HTTP_METHODS.DELETE) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.UNREGISTER;
        }
        if (httpMethodType === HTTP_METHODS.GET) {
            return MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_GET;
        }
        log.warn(`Unrecognized httpMethod for devices/{deviceId} - uri: ${uri}, httpMethod: ${httpMethodType}`, logContext);
        return MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
    }
    if (uri.includes('/devices') && !uri.match(/\/devices\/[^/?]+/)) {
        return MOBIUS_SOCKET_MESSAGE_TYPE.DEVICE_LIST;
    }
    log.warn(`Unrecognized uri pattern for Mobius socket - uri: ${uri}, httpMethod: ${httpMethodType}`, logContext);
    return MOBIUS_SOCKET_MESSAGE_TYPE.UNKNOWN;
}
export const isSupplementaryServiceMessageType = (type) => [
    MOBIUS_SOCKET_MESSAGE_TYPE.CALL_HOLD,
    MOBIUS_SOCKET_MESSAGE_TYPE.CALL_RESUME,
    MOBIUS_SOCKET_MESSAGE_TYPE.CALL_TRANSFER,
].includes(type);
