import { WidgetType } from '@codemirror/view';
import '../../../md/chip/ui-chip.js';
/**
 * A widget that represents a placeholder in the code editor,
 * specifically for suggestions that are replaced with chips.
 *
 * This widget is used to create a visual representation of a suggestion
 * in the code editor, allowing users to see and interact with suggestions
 * as chips. When a chip is removed, the corresponding text in the editor
 * is also removed.
 */
export class ChipWidget extends WidgetType {
    suggestion;
    constructor(suggestion) {
        super();
        this.suggestion = suggestion;
    }
    eq(other) {
        return this.suggestion.id == other.suggestion.id;
    }
    toDOM(view) {
        const wrapper = document.createElement('span');
        wrapper.className = 'mention-chip';
        wrapper.setAttribute('data-mention-id', this.suggestion.id);
        const chip = document.createElement('ui-chip');
        chip.setAttribute('type', 'input');
        chip.setAttribute('removable', 'true');
        chip.textContent = this.suggestion.label;
        chip.addEventListener('remove', () => {
            const pos = view.posAtDOM(wrapper);
            if (pos === null)
                return;
            const originalText = `{{${this.suggestion.label}}}`;
            view.dispatch({
                changes: { from: pos, to: pos + originalText.length, insert: '' },
            });
            view.focus();
        });
        wrapper.appendChild(chip);
        return wrapper;
    }
    ignoreEvent() {
        return false;
    }
}
//# sourceMappingURL=PlaceholderWidget.js.map