import { createLinkElementFromAnchor } from "../field-types/richtext/utils/conversion";
/**
 * Builds the LinkEditorDialog value for the anchor the canvas selection is
 * inside, so re-opening the link dialog on the WYSIWYG toolbar hydrates the
 * current link the same way the form editor does. Uses the rich text
 * converter's anchor parsing, so internal-link detection (dynamic links,
 * data attributes, editor-local hrefs) cannot drift between the two editors.
 */
export function buildLinkValueFromBridgeAttributes(attributes) {
    if (!attributes?.href || typeof document === "undefined") {
        return null;
    }
    const anchor = document.createElement("a");
    anchor.setAttribute("href", attributes.href);
    if (attributes.target)
        anchor.setAttribute("target", attributes.target);
    if (attributes.title)
        anchor.setAttribute("title", attributes.title);
    if (attributes.rel)
        anchor.setAttribute("rel", attributes.rel);
    if (attributes.dataInternal) {
        anchor.setAttribute("data-internal", attributes.dataInternal);
    }
    if (attributes.dataItemId) {
        anchor.setAttribute("data-item-id", attributes.dataItemId);
    }
    if (attributes.dataQuerystring) {
        anchor.setAttribute("data-querystring", attributes.dataQuerystring);
    }
    const linkElement = createLinkElementFromAnchor(anchor, []);
    const link = linkElement.link;
    if (!link) {
        return null;
    }
    if (link.type === "internal") {
        return {
            type: "internal",
            itemId: link.itemId || link.targetItemLongId?.split("/").pop() || "",
            targetItemLongId: link.targetItemLongId || "",
            target: link.target === "_self" ? "" : link.target || "",
            queryString: link.queryString || "",
            title: link.title || "",
            nofollow: !!link.nofollow,
        };
    }
    return {
        type: "external",
        url: linkElement.url || link.url || "",
        target: link.target === "_self" ? "" : link.target || "",
        queryString: link.queryString || "",
        title: link.title || "",
        nofollow: !!link.nofollow,
    };
}
//# sourceMappingURL=bridgeLinkValue.js.map