import { getRegisteredAccessToken } from '../tokenAccess';

const CHAT_API_RESOURCE_MARKER = '/api/chat/resources/';
const CHAT_DOT_CODEX_MARKER = '/.codex_chat/';
const CHAT_PUBLIC_DOT_CODEX_MARKER = '/public/.codex_chat/';
const RESOURCE_MANAGER_PREVIEW_MARKER = '/api/resource-manager/preview/';
const RESOURCE_MANAGER_ROOT_MARKER = 'resource/';

function normalizeResourceManagerPathSegment(segment: string) {
  const normalized = String(segment ?? '').trim();

  if (!normalized) {
    return '';
  }

  try {
    return encodeURIComponent(decodeURIComponent(normalized));
  } catch {
    return encodeURIComponent(normalized);
  }
}

function buildResourceManagerPreviewPath(value: string) {
  const normalized = String(value ?? '').trim().replace(/\\/g, '/');
  const matchedResourcePath = normalized.match(/(?:^|\/)(resource\/.+)$/i)?.[1];
  const resourcePath = String(matchedResourcePath ?? '').trim().replace(/^\/+/, '');

  if (!resourcePath) {
    return '';
  }

  const relativePath = resourcePath.slice(RESOURCE_MANAGER_ROOT_MARKER.length).replace(/^\/+/, '');

  if (!relativePath) {
    return '';
  }

  const encodedPath = relativePath
    .split('/')
    .filter(Boolean)
    .map((segment) => normalizeResourceManagerPathSegment(segment))
    .join('/');

  return encodedPath ? `${RESOURCE_MANAGER_PREVIEW_MARKER}${encodedPath}` : '';
}

function resolveLegacyExternalImageUrl(value: string) {
  const normalized = String(value ?? '').trim();
  const match = normalized.match(/^\/api\/resource-manager\/preview\/(\d+)\/([^/?#]+\.(?:png|jpe?g|gif|webp|svg|bmp|ico))(?:[?#].*)?$/i);

  if (!match) {
    return '';
  }

  const [, directory, fileName] = match;

  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/${directory}/${fileName}`;
}

function extractEmbeddedResourcePath(value: string) {
  const normalized = String(value ?? '').trim();

  if (!normalized) {
    return '';
  }

  const apiMarkerIndex = normalized.lastIndexOf(CHAT_API_RESOURCE_MARKER);

  if (apiMarkerIndex >= 0) {
    const apiPath = normalized.slice(apiMarkerIndex);
    const dotCodexIndex = apiPath.indexOf(CHAT_DOT_CODEX_MARKER);
    return dotCodexIndex >= 0
      ? `${CHAT_API_RESOURCE_MARKER}${apiPath.slice(dotCodexIndex + 1)}`
      : apiPath;
  }

  const publicDotCodexIndex = normalized.lastIndexOf(CHAT_PUBLIC_DOT_CODEX_MARKER);

  if (publicDotCodexIndex >= 0) {
    return `${CHAT_API_RESOURCE_MARKER}${normalized.slice(publicDotCodexIndex + 8)}`;
  }

  const dotCodexIndex = normalized.lastIndexOf(CHAT_DOT_CODEX_MARKER);

  if (dotCodexIndex >= 0) {
    return `${CHAT_API_RESOURCE_MARKER}${normalized.slice(dotCodexIndex + 1)}`;
  }

  if (normalized === 'resource' || normalized === '/resource' || normalized.startsWith('resource/') || normalized.includes('/resource/')) {
    const resourceManagerPreviewPath = buildResourceManagerPreviewPath(normalized);

    if (resourceManagerPreviewPath) {
      return resourceManagerPreviewPath;
    }
  }

  return normalized;
}

function extractKnownPreviewPath(value: string) {
  const normalized = String(value ?? '').trim();

  if (!normalized) {
    return '';
  }

  try {
    const parsed = new URL(normalized);
    const pathname = `${parsed.pathname || ''}${parsed.search || ''}${parsed.hash || ''}`;

    if (pathname.startsWith(CHAT_API_RESOURCE_MARKER) || pathname.startsWith(RESOURCE_MANAGER_PREVIEW_MARKER)) {
      return pathname;
    }

    return normalized;
  } catch {
    return extractEmbeddedResourcePath(normalized);
  }
}

export function normalizeChatResourceUrl(value: string) {
  const normalized = extractKnownPreviewPath(value);
  const legacyExternalImageUrl = resolveLegacyExternalImageUrl(normalized);

  if (legacyExternalImageUrl) {
    return legacyExternalImageUrl;
  }

  if (typeof window === 'undefined') {
    return normalized;
  }

  try {
    const resolvedUrl = new URL(normalized, window.location.href);

    if (resolvedUrl.pathname.startsWith(RESOURCE_MANAGER_PREVIEW_MARKER) && !resolvedUrl.searchParams.has('token')) {
      const token = getRegisteredAccessToken();

      if (token) {
        resolvedUrl.searchParams.set('token', token);
      }
    }

    return resolvedUrl.toString();
  } catch {
    return normalized;
  }
}

export function isMarkdownResourceUrl(value?: string | null) {
  const normalized = String(value ?? '').trim();

  if (!normalized) {
    return false;
  }

  try {
    const resolvedUrl = new URL(normalized, 'http://localhost');
    return /\.(md|markdown)$/i.test(resolvedUrl.pathname);
  } catch {
    return /\.(md|markdown)(?:$|[?#])/i.test(normalized);
  }
}

export function isMarkdownContentType(contentType?: string | null) {
  if (!contentType) {
    return false;
  }

  const normalized = contentType.toLowerCase();

  return (
    normalized.includes('text/markdown') ||
    normalized.includes('text/x-markdown') ||
    normalized.includes('application/markdown')
  );
}
