// Copyright (c) 2026 Modelware. All rights reserved.
import MarkdownIt from 'markdown-it';
import { displayLabelFromIri } from './renderer.js';
const INLINE_MARKDOWN_RENDERER = new MarkdownIt({
    html: true,
    linkify: true,
    typographer: false,
});
export function appendInlineValue(container, value) {
    const rendered = INLINE_MARKDOWN_RENDERER.renderInline(value);
    if (!rendered) {
        return;
    }
    const template = document.createElement('template');
    template.innerHTML = rendered;
    container.append(...Array.from(template.content.childNodes));
}
export function appendTokenizedValueParts(container, raw, partClass) {
    const tokens = splitTokenizedValueParts(raw);
    if (tokens.length === 0) {
        container.textContent = raw;
        return;
    }
    for (const token of tokens) {
        const fragment = document.createElement('span');
        fragment.className = partClass;
        const wikilinkIri = parseWikilinkToken(token);
        if (wikilinkIri) {
            fragment.dataset.value = wikilinkIri;
            fragment.appendChild(createWikiLinkElement(wikilinkIri, shortLabelFromIri(wikilinkIri)));
            container.appendChild(fragment);
            continue;
        }
        fragment.dataset.value = token;
        if (isUrlValue(token)) {
            fragment.appendChild(createExternalLinkElement(token));
        }
        else {
            appendInlineValue(fragment, token);
        }
        container.appendChild(fragment);
    }
}
export function splitTokenizedValueParts(raw) {
    return raw
        .split(/\r?\n|,\s*/)
        .map((entry) => entry.trim())
        .filter((entry) => entry.length > 0);
}
export function shortLabelFromIri(iri) {
    return displayLabelFromIri(iri);
}
export function isIriValue(value) {
    return /^[a-z][a-z0-9+.-]*:[^\s]+$/i.test(value);
}
export function createWikiLinkElement(iri, label) {
    const link = document.createElement('a');
    link.className = 'wikilink';
    link.href = '#';
    link.setAttribute('iri', iri);
    link.title = iri;
    link.textContent = label;
    return link;
}
export function isUrlValue(value) {
    const trimmed = value.trim();
    if (!trimmed) {
        return false;
    }
    try {
        const parsed = new URL(trimmed);
        return parsed.protocol.length > 1;
    }
    catch {
        return false;
    }
}
function createExternalLinkElement(href, label) {
    const link = document.createElement('a');
    link.href = href;
    link.textContent = label ?? href;
    return link;
}
function parseWikilinkToken(value) {
    const match = /^\[\[([^\]]+)\]\]$/.exec(value);
    if (!match) {
        return undefined;
    }
    const iri = (match[1] ?? '').trim();
    return iri.length > 0 ? iri : undefined;
}
const wikilinkRule = (state, silent) => {
    const { src } = state;
    const start = state.pos;
    if (src.charCodeAt(start) !== 0x5b || src.charCodeAt(start + 1) !== 0x5b) {
        return false;
    }
    const close = src.indexOf(']]', start + 2);
    if (close < 0) {
        return false;
    }
    const iri = src.slice(start + 2, close).trim();
    if (!iri) {
        return false;
    }
    if (!silent) {
        const label = shortLabelFromIri(iri);
        const open = state.push('link_open', 'a', 1);
        open.attrSet('class', 'wikilink');
        open.attrSet('iri', iri);
        open.attrSet('href', '#');
        open.attrSet('title', iri);
        const text = state.push('text', '', 0);
        text.content = label;
        state.push('link_close', 'a', -1);
    }
    state.pos = close + 2;
    return true;
};
INLINE_MARKDOWN_RENDERER.inline.ruler.before('link', 'wiki-link', wikilinkRule);
//# sourceMappingURL=wikilink-utils.js.map