"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSchemaSpecs = void 0;
const prosemirror_model_1 = require("prosemirror-model");
const const_1 = require("./const.js");
const DEFAULT_LABEL_PLACEHOLDER = 'Checkbox';
const getSchemaSpecs = (opts, placeholder) => ({
    [const_1.CheckboxNode.Checkbox]: {
        group: 'block checkbox',
        content: `${const_1.CheckboxNode.Input} ${const_1.CheckboxNode.Label}`,
        selectable: true,
        allowSelection: false,
        attrs: {
            [const_1.CheckboxAttr.Class]: { default: (0, const_1.b)(null, 'checkbox') },
            [const_1.CheckboxAttr.Line]: { default: null },
            ...(opts?.multiline ? { [const_1.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 prosemirror_model_1.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 prosemirror_model_1.Fragment.empty;
                },
            },
        ],
        toDOM(node) {
            return ['div', node.attrs, 0];
        },
        complex: 'root',
    },
    [const_1.CheckboxNode.Input]: {
        group: 'block checkbox',
        parseDOM: [],
        attrs: {
            [const_1.CheckboxAttr.Type]: { default: 'checkbox' },
            [const_1.CheckboxAttr.Id]: { default: null },
            [const_1.CheckboxAttr.Checked]: { default: null },
        },
        toDOM(node) {
            return ['div', node.attrs];
        },
        selectable: false,
        allowSelection: false,
        complex: 'leaf',
    },
    [const_1.CheckboxNode.Label]: {
        content: 'inline*',
        group: 'block checkbox',
        parseDOM: [
            {
                tag: `span[class="${(0, const_1.b)('label')}"]`,
                getAttrs: (node) => ({
                    [const_1.CheckboxAttr.For]: node.getAttribute(const_1.CheckboxAttr.For) || '',
                }),
            },
            {
                // input handled by checkbox node parse rule
                // ignore label
                tag: 'input[type=checkbox] ~ label',
                ignore: true,
                consuming: true,
            },
        ],
        attrs: {
            [const_1.CheckboxAttr.For]: { default: null },
        },
        escapeText: false,
        placeholder: {
            content: placeholder?.[const_1.CheckboxNode.Label] ??
                opts?.checkboxLabelPlaceholder ??
                DEFAULT_LABEL_PLACEHOLDER,
            alwaysVisible: true,
        },
        toDOM(node) {
            return ['span', { ...node.attrs, class: (0, const_1.b)('label') }, 0];
        },
        selectable: false,
        allowSelection: false,
        disableGapCursor: true,
        canContainBreaks: Boolean(opts?.multiline),
        complex: 'leaf',
    },
});
exports.getSchemaSpecs = getSchemaSpecs;
// 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 prosemirror_model_1.Fragment.from([
        (0, const_1.checkboxInputType)(schema).create({ [const_1.CheckboxAttr.Checked]: checked ? 'true' : null }),
        (0, const_1.checkboxLabelType)(schema).create(null, labelText ? schema.text(labelText) : null),
    ]);
}
//# sourceMappingURL=schema.js.map
