import { Fragment } from 'prosemirror-model';
import { CheckboxAttr, CheckboxNode, b, checkboxInputType, checkboxLabelType } from "./const.js";
const DEFAULT_LABEL_PLACEHOLDER = 'Checkbox';
export const getSchemaSpecs = (opts, placeholder) => ({
    [CheckboxNode.Checkbox]: {
        group: 'block checkbox',
        content: `${CheckboxNode.Input} ${CheckboxNode.Label}`,
        selectable: true,
        allowSelection: false,
        attrs: {
            [CheckboxAttr.Class]: { default: b(null, 'checkbox') },
            [CheckboxAttr.Line]: { default: null },
            ...(opts?.multiline ? { [CheckboxAttr.Tight]: { default: null } } : undefined),
        },
        parseDOM: [
            {
                tag: 'div.checkbox',
                priority: 100,
                getContent(node, schema) {
                    if (node instanceof HTMLElement) {
                        const input = node.querySelector('input[type=checkbox]');
                        if (input && input instanceof HTMLInputElement) {
                            const label = findLabelForInput(input);
                            return createCheckboxFragment(schema, input.checked, label?.textContent);
                        }
                    }
                    return Fragment.empty;
                },
            },
            {
                tag: 'input[type=checkbox]',
                priority: 50,
                getContent(input, schema) {
                    if (input instanceof HTMLInputElement) {
                        const label = findLabelForInput(input);
                        return createCheckboxFragment(schema, input.checked, label?.textContent);
                    }
                    return Fragment.empty;
                },
            },
        ],
        toDOM(node) {
            return ['div', node.attrs, 0];
        },
        complex: 'root',
    },
    [CheckboxNode.Input]: {
        group: 'block checkbox',
        parseDOM: [],
        attrs: {
            [CheckboxAttr.Type]: { default: 'checkbox' },
            [CheckboxAttr.Id]: { default: null },
            [CheckboxAttr.Checked]: { default: null },
        },
        toDOM(node) {
            return ['div', node.attrs];
        },
        selectable: false,
        allowSelection: false,
        complex: 'leaf',
    },
    [CheckboxNode.Label]: {
        content: 'inline*',
        group: 'block checkbox',
        parseDOM: [
            {
                tag: `span[class="${b('label')}"]`,
                getAttrs: (node) => ({
                    [CheckboxAttr.For]: node.getAttribute(CheckboxAttr.For) || '',
                }),
            },
            {
                // input handled by checkbox node parse rule
                // ignore label
                tag: 'input[type=checkbox] ~ label',
                ignore: true,
                consuming: true,
            },
        ],
        attrs: {
            [CheckboxAttr.For]: { default: null },
        },
        escapeText: false,
        placeholder: {
            content: placeholder?.[CheckboxNode.Label] ??
                opts?.checkboxLabelPlaceholder ??
                DEFAULT_LABEL_PLACEHOLDER,
            alwaysVisible: true,
        },
        toDOM(node) {
            return ['span', { ...node.attrs, class: b('label') }, 0];
        },
        selectable: false,
        allowSelection: false,
        disableGapCursor: true,
        canContainBreaks: Boolean(opts?.multiline),
        complex: 'leaf',
    },
});
// fallback for invalid HTML (input without id + label without for)
function findNextSiblingLabel(element) {
    const nextSibling = element.nextElementSibling;
    return nextSibling instanceof HTMLLabelElement ? nextSibling : null;
}
function findLabelForInput(element) {
    return element.labels?.[0] || findNextSiblingLabel(element);
}
function createCheckboxFragment(schema, checked, labelText) {
    return Fragment.from([
        checkboxInputType(schema).create({ [CheckboxAttr.Checked]: checked ? 'true' : null }),
        checkboxLabelType(schema).create(null, labelText ? schema.text(labelText) : null),
    ]);
}
//# sourceMappingURL=schema.js.map
