const BULLET_PREFIX = /^(\s*)-\s/;
const NUMBERED_PREFIX = /^(\s*)\d+\.\s/;
const NUMBERED_PREFIX_FULL = /^(\s*)(\d+)\.\s/;
function getAffectedLines(value, selectionStart, selectionEnd) {
    const lineStart = value.lastIndexOf('\n', selectionStart - 1) + 1;
    let lineEnd = value.indexOf('\n', selectionEnd);
    if (-1 === lineEnd) lineEnd = value.length;
    const linesText = value.slice(lineStart, lineEnd);
    const lines = linesText.split('\n');
    return {
        lineStart,
        lineEnd,
        lines
    };
}
function toggleBulletList(input) {
    const { value, selectionStart, selectionEnd } = input;
    const { lineStart, lineEnd, lines } = getAffectedLines(value, selectionStart, selectionEnd);
    const allHaveBullet = lines.every((line)=>BULLET_PREFIX.test(line));
    let newLines;
    let prefixDelta;
    if (allHaveBullet) {
        newLines = lines.map((line)=>line.replace(BULLET_PREFIX, '$1'));
        prefixDelta = lines[0].length - newLines[0].length;
    } else {
        newLines = lines.map((line)=>{
            if (BULLET_PREFIX.test(line)) return line;
            if (NUMBERED_PREFIX.test(line)) return line.replace(NUMBERED_PREFIX, '$1- ');
            return `- ${line}`;
        });
        prefixDelta = newLines[0].length - lines[0].length;
    }
    const newLinesText = newLines.join('\n');
    const newValue = value.slice(0, lineStart) + newLinesText + value.slice(lineEnd);
    const totalLengthDelta = newLinesText.length - (lineEnd - lineStart);
    const isMultiLine = lines.length > 1;
    const newStart = allHaveBullet ? Math.max(lineStart, selectionStart - prefixDelta) : isMultiLine ? selectionStart : selectionStart + prefixDelta;
    const newEnd = selectionEnd + totalLengthDelta;
    return {
        value: newValue,
        selectionStart: newStart,
        selectionEnd: Math.max(newStart, newEnd)
    };
}
function toggleNumberedList(input) {
    const { value, selectionStart, selectionEnd } = input;
    const { lineStart, lineEnd, lines } = getAffectedLines(value, selectionStart, selectionEnd);
    const allHaveNumbered = lines.every((line)=>NUMBERED_PREFIX.test(line));
    let newLines;
    let prefixDelta;
    if (allHaveNumbered) {
        newLines = lines.map((line)=>line.replace(NUMBERED_PREFIX, '$1'));
        prefixDelta = lines[0].length - newLines[0].length;
    } else {
        newLines = lines.map((line, i)=>{
            const num = `${i + 1}. `;
            if (NUMBERED_PREFIX.test(line)) return line.replace(NUMBERED_PREFIX, `$1${num}`);
            if (BULLET_PREFIX.test(line)) return line.replace(BULLET_PREFIX, `$1${num}`);
            return `${num}${line}`;
        });
        prefixDelta = newLines[0].length - lines[0].length;
    }
    const newLinesText = newLines.join('\n');
    const newValue = value.slice(0, lineStart) + newLinesText + value.slice(lineEnd);
    const totalLengthDelta = newLinesText.length - (lineEnd - lineStart);
    const newStart = allHaveNumbered ? Math.max(lineStart, selectionStart - prefixDelta) : selectionStart + prefixDelta;
    const newEnd = selectionEnd + totalLengthDelta;
    return {
        value: newValue,
        selectionStart: newStart,
        selectionEnd: Math.max(newStart, newEnd)
    };
}
function continueListOnEnter(input) {
    const { value, selectionStart, selectionEnd } = input;
    if (selectionStart !== selectionEnd) return null;
    const lineStart = value.lastIndexOf('\n', selectionStart - 1) + 1;
    let lineEnd = value.indexOf('\n', selectionStart);
    if (-1 === lineEnd) lineEnd = value.length;
    const currentLine = value.slice(lineStart, lineEnd);
    const bulletMatch = BULLET_PREFIX.exec(currentLine);
    if (bulletMatch) {
        const indent = bulletMatch[1];
        const prefix = `${indent}- `;
        const contentAfterPrefix = currentLine.slice(bulletMatch[0].length);
        if ('' === contentAfterPrefix.trim() && selectionStart === lineStart + currentLine.length) {
            const newValue = value.slice(0, lineStart) + value.slice(lineEnd);
            return {
                value: newValue,
                selectionStart: lineStart,
                selectionEnd: lineStart
            };
        }
        const newValue = `${value.slice(0, selectionStart)}\n${prefix}${value.slice(selectionStart)}`;
        const newCursor = selectionStart + 1 + prefix.length;
        return {
            value: newValue,
            selectionStart: newCursor,
            selectionEnd: newCursor
        };
    }
    const numberedMatch = NUMBERED_PREFIX_FULL.exec(currentLine);
    if (numberedMatch) {
        const indent = numberedMatch[1];
        const currentNum = parseInt(numberedMatch[2], 10);
        const contentAfterPrefix = currentLine.slice(numberedMatch[0].length);
        if ('' === contentAfterPrefix.trim() && selectionStart === lineStart + currentLine.length) {
            const newValue = value.slice(0, lineStart) + value.slice(lineEnd);
            return {
                value: newValue,
                selectionStart: lineStart,
                selectionEnd: lineStart
            };
        }
        const prefix = `${indent}${currentNum + 1}. `;
        const newValue = `${value.slice(0, selectionStart)}\n${prefix}${value.slice(selectionStart)}`;
        const newCursor = selectionStart + 1 + prefix.length;
        return {
            value: newValue,
            selectionStart: newCursor,
            selectionEnd: newCursor
        };
    }
    return null;
}
export { BULLET_PREFIX, NUMBERED_PREFIX, NUMBERED_PREFIX_FULL, continueListOnEnter, toggleBulletList, toggleNumberedList };
