import { Editor, ViewElement } from '../../ckeditor/5-43.3.1/ckeditor5.js';

/**
 * @param {Editor} editor
 */
export function CastersMencoes(editor) {
    editor.conversion.for('upcast').elementToAttribute({
        view: {
            name: 'a',
            key: 'data-mention',
            classes: 'govbr-editor-html-mencao',
            attributes: {
                'href': true,
                'data-nome': true,
            },
        },
        model: {
            key: 'mention',
            /**
             * @param {ViewElement} viewItem
             */
            value: (viewItem) => {
                return editor.plugins.get('Mention').toMentionAttribute(viewItem, {
                    link: viewItem.getAttribute('href'),
                    nome: viewItem.getAttribute('data-nome'),
                    naoDestacar: viewItem.hasClass('govbr-editor-html-mencao-nao-destacar'),
                    conteudoHTML: viewItem.innerHTML,
                });
            },
        },
        converterPriority: 'high',
    });
    editor.conversion.for('upcast').elementToAttribute({
        view: {
            name: 'span',
            key: 'data-mention',
            classes: 'govbr-editor-html-mencao',
            attributes: {
                'data-nome': true,
            },
        },
        model: {
            key: 'mention',
            /**
             * @param {ViewElement} viewItem
             */
            value: (viewItem) => {
                return editor.plugins.get('Mention').toMentionAttribute(viewItem, {
                    nome: viewItem.getAttribute('data-nome'),
                    naoDestacar: viewItem.hasClass('govbr-editor-html-mencao-nao-destacar'),
                    conteudoHTML: viewItem.innerHTML,
                });
            },
        },
        converterPriority: 'high',
    });

    editor.conversion.for('downcast').attributeToElement({
        model: 'mention',
        /**
         * @param {*} modelAttributeValue
         * @param {any} _
         */
        view: (modelAttributeValue, { writer }) => {
            if (!modelAttributeValue) {
                return;
            }

            if (modelAttributeValue.link) {
                return writer.createAttributeElement(
                    'a',
                    {
                        'class': 'govbr-editor-html-mencao' + (modelAttributeValue.naoDestacar ? ' govbr-editor-html-mencao-nao-destacar' : ''),
                        'data-mention': modelAttributeValue.id,
                        'data-nome': modelAttributeValue.nome,
                        'href': modelAttributeValue.link,
                        'title': modelAttributeValue.nome,
                    },
                    {
                        priority: 20,
                        id: modelAttributeValue.uid,
                    }
                );
            }

            return writer.createAttributeElement(
                'span',
                {
                    'class': 'govbr-editor-html-mencao' + (modelAttributeValue.naoDestacar ? ' govbr-editor-html-mencao-nao-destacar' : ''),
                    'data-mention': modelAttributeValue.id,
                    'data-nome': modelAttributeValue.nome,
                    'title': modelAttributeValue.nome,
                },
                {
                    priority: 20,
                    id: modelAttributeValue.uid,
                }
            );
        },
        converterPriority: 'high',
    });
}
