"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    findStickyNoteMediaAtSelection: ()=>findStickyNoteMediaAtSelection,
    insertStickyNoteMedia: ()=>insertStickyNoteMedia,
    parseStickyNoteMediaMarker: ()=>parseStickyNoteMediaMarker,
    parseStickyNoteMediaSource: ()=>parseStickyNoteMediaSource,
    parseStickyNoteMediaTokens: ()=>parseStickyNoteMediaTokens,
    parseStickyNoteMediaUrl: ()=>parseStickyNoteMediaUrl,
    replaceStickyNoteMedia: ()=>replaceStickyNoteMedia,
    serializeStickyNoteMedia: ()=>serializeStickyNoteMedia
});
const YOUTUBE_VIDEO_ID = /^[A-Za-z0-9_-]{11}$/;
const YOUTUBE_HOSTS = new Set([
    'youtube.com',
    'www.youtube.com',
    'm.youtube.com',
    'music.youtube.com',
    'youtu.be',
    'www.youtu.be',
    'youtube-nocookie.com',
    'www.youtube-nocookie.com'
]);
const PAGE_BASED_VIDEO_HOSTS = new Set([
    'vimeo.com',
    'www.vimeo.com',
    'player.vimeo.com',
    'loom.com',
    'www.loom.com'
]);
const MEDIA_TITLE_PREFIX = 'sticky-note-media';
const MEDIA_TITLE_PATTERN = /^sticky-note-media;kind=(image|youtube|publicVideo);layout=(natural-width|full-width)$/;
const FLOW_MEDIA_TITLE_PATTERN = /^flow-media:(image|youtube|video):(natural-width|full-width)$/;
const LEGACY_FULL_WIDTH_IMAGE_TITLE = 'flow-full-width';
function parseHttpsUrl(rawUrl) {
    let url;
    try {
        url = new URL(rawUrl.trim());
    } catch  {
        return {
            ok: false,
            error: 'invalid-url'
        };
    }
    if ('https:' !== url.protocol) return {
        ok: false,
        error: 'https-required'
    };
    if (url.username || url.password) return {
        ok: false,
        error: 'invalid-url'
    };
    return {
        ok: true,
        url
    };
}
function youtubeVideoId(url) {
    const hostname = url.hostname.toLowerCase();
    let candidate = null;
    if ('youtu.be' === hostname || 'www.youtu.be' === hostname) candidate = url.pathname.split('/').filter(Boolean)[0] ?? null;
    else if ('/watch' === url.pathname) candidate = url.searchParams.get('v');
    else {
        const [kind, id] = url.pathname.split('/').filter(Boolean);
        if ('embed' === kind || 'live' === kind || 'shorts' === kind) candidate = id ?? null;
    }
    return candidate && YOUTUBE_VIDEO_ID.test(candidate) ? candidate : null;
}
function parseStickyNoteMediaUrl(rawUrl, mediaType, options = {}) {
    const parsed = parseHttpsUrl(rawUrl);
    if (!parsed.ok) return parsed;
    const { url } = parsed;
    const hostname = url.hostname.toLowerCase();
    const fullWidth = options.fullWidth ?? false;
    if ('image' === mediaType) {
        if (YOUTUBE_HOSTS.has(hostname)) return {
            ok: false,
            error: 'invalid-url'
        };
        return {
            ok: true,
            value: {
                kind: 'image',
                url: url.toString(),
                alt: options.alt ?? '',
                fullWidth
            }
        };
    }
    if (YOUTUBE_HOSTS.has(hostname)) {
        const videoId = youtubeVideoId(url);
        return videoId ? {
            ok: true,
            value: {
                kind: 'youtube',
                videoId,
                fullWidth
            }
        } : {
            ok: false,
            error: 'invalid-youtube-url'
        };
    }
    if (hostname.includes('youtube.') || hostname.includes('youtu.be')) return {
        ok: false,
        error: 'invalid-url'
    };
    if (PAGE_BASED_VIDEO_HOSTS.has(hostname)) return {
        ok: false,
        error: 'unsupported-video-url'
    };
    return {
        ok: true,
        value: {
            kind: 'publicVideo',
            url: url.toString(),
            fullWidth
        }
    };
}
function escapeMarkdownLabel(value) {
    return value.replace(/\r\n?|\n/g, ' ').replace(/\\/g, '\\\\').replace(/\[/g, '\\[').replace(/\]/g, '\\]');
}
function serializeStickyNoteMedia(media) {
    const layout = media.fullWidth ? 'full-width' : 'natural-width';
    const title = `${MEDIA_TITLE_PREFIX};kind=${media.kind};layout=${layout}`;
    if ('youtube' === media.kind) return `![](<https://www.youtube.com/watch?v=${media.videoId}> "${title}")`;
    const label = 'image' === media.kind ? escapeMarkdownLabel(media.alt) : '';
    return `![${label}](<${new URL(media.url).toString()}> "${title}")`;
}
function parseStickyNoteMediaMarker(title) {
    if (title === LEGACY_FULL_WIDTH_IMAGE_TITLE) return {
        kind: 'image',
        fullWidth: true
    };
    const marker = title?.match(MEDIA_TITLE_PATTERN);
    const flowMarker = title?.match(FLOW_MEDIA_TITLE_PATTERN);
    const kind = marker?.[1] ?? flowMarker?.[1];
    const layout = marker?.[2] ?? flowMarker?.[2];
    if (!kind || !layout) return null;
    const normalizedKind = 'video' === kind ? 'publicVideo' : 'image' === kind || 'youtube' === kind || 'publicVideo' === kind ? kind : null;
    if (!normalizedKind) return null;
    return {
        kind: normalizedKind,
        fullWidth: 'full-width' === layout
    };
}
function mediaTypeForKind(kind) {
    return 'image' === kind ? 'image' : 'video';
}
function parseStickyNoteMediaSource(source, alt = '', title) {
    const marker = parseStickyNoteMediaMarker(title);
    if (!marker) return null;
    const result = parseStickyNoteMediaUrl(source, mediaTypeForKind(marker.kind), {
        alt,
        fullWidth: marker.fullWidth
    });
    return result.ok && result.value.kind === marker.kind ? result.value : null;
}
function normalizedRange(length, selectionStart, selectionEnd) {
    const start = Math.max(0, Math.min(length, Math.min(selectionStart, selectionEnd)));
    const end = Math.max(0, Math.min(length, Math.max(selectionStart, selectionEnd)));
    return [
        start,
        end
    ];
}
function separatorAfter(prefix) {
    if (!prefix || prefix.endsWith('\n\n')) return '';
    return prefix.endsWith('\n') ? '\n' : '\n\n';
}
function separatorBefore(suffix) {
    if (!suffix || suffix.startsWith('\n\n')) return '';
    return suffix.startsWith('\n') ? '\n' : '\n\n';
}
function insertStickyNoteMedia(currentValue, block, selection) {
    const selectionIsCurrent = selection?.value === currentValue;
    const [start, end] = selectionIsCurrent ? normalizedRange(currentValue.length, selection.selectionStart, selection.selectionEnd) : [
        currentValue.length,
        currentValue.length
    ];
    const before = currentValue.slice(0, start);
    const after = currentValue.slice(end);
    const leadingSeparator = separatorAfter(before);
    const trailingSeparator = separatorBefore(after);
    const value = `${before}${leadingSeparator}${block}${trailingSeparator}${after}`;
    const caret = before.length + leadingSeparator.length + block.length + trailingSeparator.length;
    return {
        value,
        selectionStart: caret,
        selectionEnd: caret
    };
}
function replaceTextRange(currentValue, selectionStart, selectionEnd, replacement) {
    const [start, end] = normalizedRange(currentValue.length, selectionStart, selectionEnd);
    const value = currentValue.slice(0, start) + replacement + currentValue.slice(end);
    const caret = start + replacement.length;
    return {
        value,
        selectionStart: caret,
        selectionEnd: caret
    };
}
function unescapeMarkdownLabel(value) {
    return value.replace(/\\(.)/g, (match, escaped)=>'\\' === escaped || '[' === escaped || ']' === escaped ? escaped : match);
}
function fencedCodeRanges(content) {
    const ranges = [];
    let openFence = null;
    let lineStart = 0;
    while(lineStart < content.length){
        let lineEnd = lineStart;
        while(lineEnd < content.length && '\n' !== content[lineEnd] && '\r' !== content[lineEnd])lineEnd += 1;
        let nextLineStart = lineEnd;
        if ('\r' === content[nextLineStart]) nextLineStart += 1;
        if ('\n' === content[nextLineStart]) nextLineStart += 1;
        const line = content.slice(lineStart, lineEnd);
        if (openFence) {
            const closingFence = new RegExp(`^ {0,3}${openFence.marker}{${openFence.length},}[ \\t]*$`);
            if (closingFence.test(line)) {
                ranges.push({
                    start: openFence.start,
                    end: nextLineStart
                });
                openFence = null;
            }
        } else {
            const openingFence = /^ {0,3}(`{3,}|~{3,})/.exec(line);
            const fence = openingFence?.[1];
            const marker = fence?.[0];
            const info = openingFence ? line.slice((openingFence.index ?? 0) + openingFence[0].length) : '';
            if (openingFence && fence && ('`' === marker || '~' === marker) && !('`' === marker && info.includes('`'))) openFence = {
                marker,
                length: fence.length,
                start: lineStart
            };
        }
        lineStart = nextLineStart;
    }
    if (openFence) ranges.push({
        start: openFence.start,
        end: content.length
    });
    return ranges;
}
function isBackslashEscaped(content, index, segmentStart) {
    let count = 0;
    for(let cursor = index - 1; cursor >= segmentStart && '\\' === content[cursor]; cursor -= 1)count += 1;
    return count % 2 === 1;
}
function inlineCodeRanges(content, start, end) {
    const ranges = [];
    let index = start;
    while(index < end){
        if ('`' !== content[index] || isBackslashEscaped(content, index, start)) {
            index += 1;
            continue;
        }
        let openingEnd = index + 1;
        while(openingEnd < end && '`' === content[openingEnd])openingEnd += 1;
        const markerLength = openingEnd - index;
        let closingStart = openingEnd;
        let found = false;
        while(closingStart < end){
            closingStart = content.indexOf('`', closingStart);
            if (closingStart < 0 || closingStart >= end) break;
            let closingEnd = closingStart + 1;
            while(closingEnd < end && '`' === content[closingEnd])closingEnd += 1;
            if (closingEnd - closingStart === markerLength) {
                ranges.push({
                    start: index,
                    end: closingEnd
                });
                index = closingEnd;
                found = true;
                break;
            }
            closingStart = closingEnd;
        }
        if (!found) index = openingEnd;
    }
    return ranges;
}
function markdownCodeRanges(content) {
    const fencedRanges = fencedCodeRanges(content);
    const ranges = [
        ...fencedRanges
    ];
    let segmentStart = 0;
    for (const fencedRange of fencedRanges){
        ranges.push(...inlineCodeRanges(content, segmentStart, fencedRange.start));
        segmentStart = fencedRange.end;
    }
    ranges.push(...inlineCodeRanges(content, segmentStart, content.length));
    return ranges.sort((left, right)=>left.start - right.start);
}
function isLineBreak(character) {
    return '\n' === character || '\r' === character;
}
function readMarkdownImageToken(content, start) {
    const failedAt = (index)=>({
            nextIndex: Math.max(start + 2, index)
        });
    const nestedCandidateAt = (index)=>'!' === content[index] && '[' === content[index + 1];
    let index = start + 2;
    const altStart = index;
    while(index < content.length && ']' !== content[index]){
        if (nestedCandidateAt(index)) return failedAt(index);
        if (isLineBreak(content[index])) return failedAt(index + 1);
        if ('\\' === content[index]) index += 1;
        index += 1;
    }
    if (']' !== content[index] || '(' !== content[index + 1]) return failedAt(index + 1);
    const alt = content.slice(altStart, index);
    index += 2;
    let source = '';
    if ('<' === content[index]) {
        const sourceStart = ++index;
        while(index < content.length && '>' !== content[index]){
            if (nestedCandidateAt(index)) return failedAt(index);
            if (isLineBreak(content[index])) return failedAt(index + 1);
            index += 1;
        }
        if ('>' !== content[index]) return failedAt(index);
        source = content.slice(sourceStart, index);
        index += 1;
    } else {
        const sourceStart = index;
        while(index < content.length && ')' !== content[index] && ' ' !== content[index] && '\t' !== content[index] && !isLineBreak(content[index])){
            if (nestedCandidateAt(index)) return failedAt(index);
            index += 1;
        }
        source = content.slice(sourceStart, index);
    }
    if (!source) return failedAt(index + 1);
    while(' ' === content[index] || '\t' === content[index])index += 1;
    let title;
    if ('"' === content[index]) {
        const titleStart = ++index;
        while(index < content.length && '"' !== content[index]){
            if (nestedCandidateAt(index)) return failedAt(index);
            if (isLineBreak(content[index])) return failedAt(index + 1);
            if ('\\' === content[index]) index += 1;
            index += 1;
        }
        if ('"' !== content[index]) return failedAt(index);
        title = content.slice(titleStart, index);
        index += 1;
        while(' ' === content[index] || '\t' === content[index])index += 1;
    }
    if (')' !== content[index]) return failedAt(index + 1);
    const end = index + 1;
    return {
        token: {
            alt,
            source,
            title,
            end
        },
        nextIndex: end
    };
}
function parseStickyNoteMediaTokens(content) {
    const tokens = [];
    const codeRanges = markdownCodeRanges(content);
    let codeRangeIndex = 0;
    let cursor = 0;
    while(cursor < content.length){
        const start = content.indexOf('![', cursor);
        if (start < 0) break;
        while(codeRanges[codeRangeIndex] && (codeRanges[codeRangeIndex]?.end ?? 1 / 0) <= start)codeRangeIndex += 1;
        const codeRange = codeRanges[codeRangeIndex];
        if (codeRange && start >= codeRange.start && start < codeRange.end) {
            cursor = Math.max(start + 2, codeRange.end);
            continue;
        }
        const parsed = readMarkdownImageToken(content, start);
        cursor = parsed.nextIndex;
        if (!parsed.token) continue;
        const media = parseStickyNoteMediaSource(parsed.token.source, unescapeMarkdownLabel(parsed.token.alt), parsed.token.title);
        if (media) tokens.push({
            media,
            start,
            end: parsed.token.end
        });
    }
    return tokens;
}
function findStickyNoteMediaAtSelection(content, selectionStart, selectionEnd) {
    const [start, end] = normalizedRange(content.length, selectionStart, selectionEnd);
    return parseStickyNoteMediaTokens(content).find((token)=>start === end ? start >= token.start && start < token.end : start < token.end && end > token.start) ?? null;
}
function sameSource(left, right) {
    if (left.kind !== right.kind) return false;
    if ('image' === left.kind && 'image' === right.kind) return left.url === right.url;
    if ('youtube' === left.kind && 'youtube' === right.kind) return left.videoId === right.videoId;
    return 'publicVideo' === left.kind && 'publicVideo' === right.kind && left.url === right.url;
}
function replaceStickyNoteMedia(content, original, replacement) {
    let closest = null;
    for (const token of parseStickyNoteMediaTokens(content))if (sameSource(token.media, original.media)) {
        if (!closest || Math.abs(token.start - original.start) < Math.abs(closest.start - original.start)) closest = token;
    }
    return closest ? replaceTextRange(content, closest.start, closest.end, replacement) : null;
}
exports.findStickyNoteMediaAtSelection = __webpack_exports__.findStickyNoteMediaAtSelection;
exports.insertStickyNoteMedia = __webpack_exports__.insertStickyNoteMedia;
exports.parseStickyNoteMediaMarker = __webpack_exports__.parseStickyNoteMediaMarker;
exports.parseStickyNoteMediaSource = __webpack_exports__.parseStickyNoteMediaSource;
exports.parseStickyNoteMediaTokens = __webpack_exports__.parseStickyNoteMediaTokens;
exports.parseStickyNoteMediaUrl = __webpack_exports__.parseStickyNoteMediaUrl;
exports.replaceStickyNoteMedia = __webpack_exports__.replaceStickyNoteMedia;
exports.serializeStickyNoteMedia = __webpack_exports__.serializeStickyNoteMedia;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "findStickyNoteMediaAtSelection",
    "insertStickyNoteMedia",
    "parseStickyNoteMediaMarker",
    "parseStickyNoteMediaSource",
    "parseStickyNoteMediaTokens",
    "parseStickyNoteMediaUrl",
    "replaceStickyNoteMedia",
    "serializeStickyNoteMedia"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
