window.EditorToolbar = {
    wrapSelection: function (prefix, suffix) {
        const t = document.querySelector('.editor-area');
        if (!t) return;
        const start = t.selectionStart, end = t.selectionEnd;
        const val = t.value, sel = val.substring(start, end);
        t.value = val.substring(0, start) + prefix + sel + suffix + val.substring(end);
        const p = start + prefix.length;
        t.selectionStart = p;
        t.selectionEnd = p + sel.length;
        t.dispatchEvent(new Event('input', { bubbles: true }));
    },
    insertHeading: function (level) {
        const t = document.querySelector('.editor-area');
        if (!t) return;
        const start = t.selectionStart;
        const val = t.value;
        let lineStart = start;
        while (lineStart > 0 && val[lineStart - 1] !== '\n') lineStart--;
        const prefix = '#'.repeat(level) + ' ';
        t.value = val.substring(0, lineStart) + prefix + val.substring(lineStart);
        const pos = lineStart + prefix.length + (start - lineStart);
        t.selectionStart = pos;
        t.selectionEnd = pos;
        t.dispatchEvent(new Event('input', { bubbles: true }));
    },
    insertList: function (ordered) {
        const t = document.querySelector('.editor-area');
        if (!t) return;
        const start = t.selectionStart, end = t.selectionEnd;
        const val = t.value;
        let sel = val.substring(start, end);
        if (sel.trim() === '') {
            sel = ordered
                ? '1. Erstes Element\n2. Zweites Element\n3. Drittes Element'
                : '- Erstes Element\n- Zweites Element\n- Drittes Element';
        } else {
            const lines = sel.split('\n');
            sel = ordered
                ? lines.map((l, i) => `${i + 1}. ${l}`).join('\n')
                : lines.map(l => `- ${l}`).join('\n');
        }
        t.value = val.substring(0, start) + sel + val.substring(end);
        t.selectionStart = start;
        t.selectionEnd = start + sel.length;
        t.dispatchEvent(new Event('input', { bubbles: true }));
    },
    indentLines: function () {
        const t = document.querySelector('.editor-area');
        if (!t) return;
        const start = t.selectionStart, end = t.selectionEnd;
        const val = t.value;
        let sel = val.substring(start, end);
        if (sel === '') return;
        sel = sel.replace(/^/gm, '    ');
        t.value = val.substring(0, start) + sel + val.substring(end);
        t.selectionStart = start;
        t.selectionEnd = start + sel.length;
        t.dispatchEvent(new Event('input', { bubbles: true }));
    },
    outdentLines: function () {
        const t = document.querySelector('.editor-area');
        if (!t) return;
        const start = t.selectionStart, end = t.selectionEnd;
        const val = t.value;
        let sel = val.substring(start, end);
        if (sel === '') return;
        sel = sel.replace(/^ {1,4}/gm, '');
        t.value = val.substring(0, start) + sel + val.substring(end);
        t.selectionStart = start;
        t.selectionEnd = start + sel.length;
        t.dispatchEvent(new Event('input', { bubbles: true }));
    }
};
