/**
 * @license MIT
 * Lorem Ipsum Generator Pro for TinyMCE 7
 * Developed by Gerits Aurélien - Magix CMS
 * Version 2.0
 */
(function () {
    'use strict';
    tinymce.PluginManager.requireLangPack("loremipsum");
    tinymce.PluginManager.add("loremipsum", function (editor, url) {

        const _ = (text) => editor.translate(text);

        const generateLorem = (count) => {
            const text = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
            let output = '';
            for (let i = 0; i < count; i++) {
                output += `<p>${text}</p>`;
            }
            return output;
        };

        const openDialog = () => {
            return editor.windowManager.open({
                title: _('Lorem Ipsum Generator'),
                body: {
                    type: 'panel',
                    items: [
                        {
                            type: 'selectbox',
                            name: 'count',
                            label: _('Number of paragraphs'),
                            items: [
                                { text: '1', value: '1' },
                                { text: '2', value: '2' },
                                { text: '3', value: '3' },
                                { text: '4', value: '4' },
                                { text: '5', value: '5' },
                                { text: '10', value: '10' },
                                { text: '15', value: '15' },
                                { text: '20', value: '20' }
                            ]
                        }
                    ]
                },
                buttons: [
                    { type: 'cancel', text: _('Cancel') },
                    { type: 'submit', text: _('Insert'), primary: true }
                ],
                initialData: {
                    count: '1'
                },
                onSubmit: (api) => {
                    const data = api.getData();
                    const count = parseInt(data.count, 10) || 1;
                    editor.insertContent(generateLorem(count));
                    api.close();
                }
            });
        };

        editor.ui.registry.addIcon('lorem-ipsum', '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 7V4a2 2 0 0 1 2-2h12a2 2 0 0 1 2 2v3"></path><path d="M9 22h6"></path><path d="M12 22V5"></path></svg>');

        editor.ui.registry.addButton('loremipsum', {
            icon: 'lorem-ipsum',
            tooltip: _('Generate Lorem Ipsum'),
            onAction: () => openDialog()
        });

        editor.ui.registry.addMenuItem('loremipsum', {
            icon: 'lorem-ipsum',
            text: _('Lorem Ipsum Generator...'),
            onAction: () => openDialog()
        });

        return {
            getMetadata: () => ({
                name: "Loremipsum Plugin",
                url:  "https://github.com/gtraxx/tinymce-lorem-ipsum",
                author: "Gerits Aurelien",
                version: "2.0"
            })
        };
    });
})();