"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)() },
        },
        parseDOM: [
            {
                tag: 'div.checkbox',
                priority: 100,
                getContent(node, schema) {
                    const input = node.querySelector('input[type=checkbox]');
                    const label = node.querySelector('label[for]');
                    const checked = input?.checked ? 'true' : null;
                    const text = label?.textContent;
                    return prosemirror_model_1.Fragment.from([
                        (0, const_1.checkboxInputType)(schema).create({ [const_1.CheckboxAttr.Checked]: checked }),
                        (0, const_1.checkboxLabelType)(schema).create(null, text ? schema.text(text) : null),
                    ]);
                },
            },
            {
                tag: 'input[type=checkbox]',
                priority: 50,
                getContent(node, schema) {
                    const id = node.id;
                    const checked = node.checked ? 'true' : null;
                    const text = node.parentNode?.querySelector(`label[for=${id}]`)?.textContent;
                    return prosemirror_model_1.Fragment.from([
                        (0, const_1.checkboxInputType)(schema).create({ [const_1.CheckboxAttr.Checked]: checked }),
                        (0, const_1.checkboxLabelType)(schema).create(null, text ? schema.text(text) : null),
                    ]);
                },
            },
        ],
        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[for]',
                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,
        complex: 'leaf',
    },
});
exports.getSchemaSpecs = getSchemaSpecs;
//# sourceMappingURL=schema.js.map
