"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getBaseFolderOfPublicPath = exports.getWSProtocolByHttpProtocol = exports.makeUrl = exports.getWsInvalidReason = exports.parseWsUrl = void 0;
const lodash_1 = require("lodash");
const enum_1 = require("@/@types/enum");
const log_1 = require("@/utils/log");
const config_1 = require("@/config");
const log = new log_1.Logger('url-utils', enum_1.WinstonColor.Gray);
/**
 * 解析 ws 连接的 url 参数
 */
const parseWsUrl = (reqUrl) => {
    const url = new URL(reqUrl, 'http://0.0.0.0');
    const clientId = url.searchParams.get('clientId');
    const contextName = url.searchParams.get('contextName');
    const clientRole = url.searchParams.get('role');
    const deviceName = url.searchParams.get('deviceName');
    const extensionName = url.searchParams.get('extensionName') || '';
    const hash = url.searchParams.get('hash') || '';
    return {
        clientId,
        clientRole,
        pathname: url.pathname,
        contextName,
        deviceName,
        extensionName,
        hash,
    };
};
exports.parseWsUrl = parseWsUrl;
/**
 * 根据 ws url 参数判断连接是否合法
 *  - 如果是 devtools 来源的 ws 连接，必须存在调试对象
 *  - 如果是 HMR 来源的 ws 连接，必须有 hash 字段
 */
const getWsInvalidReason = (wsUrlParams) => {
    const { clientRole, pathname } = wsUrlParams;
    log.info('clientRole: %s', clientRole);
    if (pathname !== config_1.config.wsPath)
        return 'invalid ws connection path!';
    if (!Object.values(enum_1.ClientRole).includes(clientRole))
        return 'invalid client role!';
    if (clientRole === enum_1.ClientRole.HMRClient || clientRole === enum_1.ClientRole.HMRServer) {
        if ('hash' in wsUrlParams)
            return '';
        return 'invalid HMR ws params, must connect with hash field!';
    }
    const { clientId, contextName } = wsUrlParams;
    // if (clientRole === ClientRole.Devtools && !('hash' in wsUrlParams) && config.isCluster)
    //   return 'invalid ws connection, you ws url should carry `hash` query params!';
    if (clientRole === enum_1.ClientRole.IOS && !contextName)
        return 'invalid iOS connection, should request with contextName!';
    if (!clientId)
        return 'invalid ws connection!';
    return '';
};
exports.getWsInvalidReason = getWsInvalidReason;
/**
 * 根据 query 对象创建 url
 */
const makeUrl = (baseUrl, query = {}) => {
    let fullUrl = baseUrl;
    const keys = Object.keys(query);
    for (const [i, key] of keys.entries()) {
        if (i === 0) {
            if (fullUrl.indexOf('?') === -1) {
                fullUrl += '?';
            }
        }
        else {
            fullUrl += '&';
        }
        fullUrl += `${key}=${encodeURIComponent(query[key])}`;
    }
    return fullUrl;
};
exports.makeUrl = makeUrl;
const getWSProtocolByHttpProtocol = (httpProtocol) => ({
    https: 'wss',
    http: 'ws',
}[httpProtocol] || 'ws');
exports.getWSProtocolByHttpProtocol = getWSProtocolByHttpProtocol;
const getBaseFolderOfPublicPath = (publicPath) => {
    const url = new URL(publicPath, 'http://0.0.0.0');
    return lodash_1.trim(url.pathname, '/');
};
exports.getBaseFolderOfPublicPath = getBaseFolderOfPublicPath;
