"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeChatResourceUrl = normalizeChatResourceUrl;
exports.isMarkdownResourceUrl = isMarkdownResourceUrl;
exports.isMarkdownContentType = isMarkdownContentType;
var CHAT_API_RESOURCE_MARKER = '/api/chat/resources/';
var CHAT_DOT_CODEX_MARKER = '/.codex_chat/';
var CHAT_PUBLIC_DOT_CODEX_MARKER = '/public/.codex_chat/';
var RESOURCE_MANAGER_PREVIEW_MARKER = '/api/resource-manager/preview/';
var RESOURCE_MANAGER_ROOT_MARKER = 'resource/';
function normalizeResourceManagerPathSegment(segment) {
    var normalized = String(segment !== null && segment !== void 0 ? segment : '').trim();
    if (!normalized) {
        return '';
    }
    try {
        return encodeURIComponent(decodeURIComponent(normalized));
    }
    catch (_a) {
        return encodeURIComponent(normalized);
    }
}
function buildResourceManagerPreviewPath(value) {
    var normalized = String(value !== null && value !== void 0 ? value : '').trim().replace(/\\/g, '/');
    var matchedResourcePath = normalized.match(/(?:^|\/)(resource\/.+)$/i);
    var resourcePath = String((matchedResourcePath === null || matchedResourcePath === void 0 ? void 0 : matchedResourcePath[1]) !== null && (matchedResourcePath === null || matchedResourcePath === void 0 ? void 0 : matchedResourcePath[1]) !== void 0 ? (matchedResourcePath === null || matchedResourcePath === void 0 ? void 0 : matchedResourcePath[1]) : '').trim().replace(/^\/+/, '');
    if (!resourcePath) {
        return '';
    }
    var relativePath = resourcePath.slice(RESOURCE_MANAGER_ROOT_MARKER.length).replace(/^\/+/, '');
    if (!relativePath) {
        return '';
    }
    var encodedPath = relativePath
        .split('/')
        .filter(Boolean)
        .map(function (segment) { return normalizeResourceManagerPathSegment(segment); })
        .join('/');
    return encodedPath ? "".concat(RESOURCE_MANAGER_PREVIEW_MARKER).concat(encodedPath) : '';
}
function resolveLegacyExternalImageUrl(value) {
    var normalized = String(value !== null && value !== void 0 ? value : '').trim();
    var match = normalized.match(/^\/api\/resource-manager\/preview\/(\d+)\/([^/?#]+\.(?:png|jpe?g|gif|webp|svg|bmp|ico))(?:[?#].*)?$/i);
    if (!match) {
        return '';
    }
    var directory = match[1], fileName = match[2];
    if (!/^\d+_image\d+[_\d-]*\.(?:png|jpe?g|gif|webp|svg|bmp|ico)$/i.test(fileName)) {
        return '';
    }
    return "https://tong.visitkorea.or.kr/cms/resource/".concat(directory, "/").concat(fileName);
}
function extractEmbeddedResourcePath(value) {
    var normalized = String(value !== null && value !== void 0 ? value : '').trim();
    if (!normalized) {
        return '';
    }
    var apiMarkerIndex = normalized.lastIndexOf(CHAT_API_RESOURCE_MARKER);
    if (apiMarkerIndex >= 0) {
        var apiPath = normalized.slice(apiMarkerIndex);
        var dotCodexIndex = apiPath.indexOf(CHAT_DOT_CODEX_MARKER);
        return dotCodexIndex >= 0
            ? "".concat(CHAT_API_RESOURCE_MARKER).concat(apiPath.slice(dotCodexIndex + 1))
            : apiPath;
    }
    var publicDotCodexIndex = normalized.lastIndexOf(CHAT_PUBLIC_DOT_CODEX_MARKER);
    if (publicDotCodexIndex >= 0) {
        return "".concat(CHAT_API_RESOURCE_MARKER).concat(normalized.slice(publicDotCodexIndex + 8));
    }
    var dotCodexIndex = normalized.lastIndexOf(CHAT_DOT_CODEX_MARKER);
    if (dotCodexIndex >= 0) {
        return "".concat(CHAT_API_RESOURCE_MARKER).concat(normalized.slice(dotCodexIndex + 1));
    }
    if (normalized === 'resource' || normalized === '/resource' || normalized.startsWith('resource/') || normalized.includes('/resource/')) {
        var resourceManagerPreviewPath = buildResourceManagerPreviewPath(normalized);
        if (resourceManagerPreviewPath) {
            return resourceManagerPreviewPath;
        }
    }
    return normalized;
}
function normalizeChatResourceUrl(value) {
    var normalized = extractKnownPreviewPath(value);
    var legacyExternalImageUrl = resolveLegacyExternalImageUrl(normalized);
    if (legacyExternalImageUrl) {
        return legacyExternalImageUrl;
    }
    if (typeof window === 'undefined') {
        return normalized;
    }
    try {
        var resolvedUrl = new URL(normalized, window.location.href);
        return resolvedUrl.toString();
    }
    catch (_a) {
        return normalized;
    }
}
function isMarkdownResourceUrl(value) {
    var normalized = String(value !== null && value !== void 0 ? value : '').trim();
    if (!normalized) {
        return false;
    }
    try {
        var resolvedUrl = new URL(normalized, 'http://localhost');
        return /\.(md|markdown)$/i.test(resolvedUrl.pathname);
    }
    catch (_a) {
        return /\.(md|markdown)(?:$|[?#])/i.test(normalized);
    }
}
function isMarkdownContentType(contentType) {
    if (!contentType) {
        return false;
    }
    var normalized = contentType.toLowerCase();
    return (normalized.includes('text/markdown') ||
        normalized.includes('text/x-markdown') ||
        normalized.includes('application/markdown'));
}
