{{!
    This file is part of Moodle - https://moodle.org/

    Moodle is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Moodle is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
}}
{{!
    @template file/tiny/plugin_js

    AMD amd/src/plugin.js file.

    Context variables required for this template:
    * component
    * copyright

    Example context (json):
    {
        "component": "tiny_example",
        "copyright": "2022 Andrew Lyons <andrew@nicols.co.uk>"
    }
}}
{{< common/boilerplate_amd_js }}
    {{$ description }}Tiny {{ component }} for Moodle.{{/ description }}
    {{$ modulename }}plugin{{/ modulename }}
    {{$ copyright }}{{ copyright }}{{/ copyright }}
{{/ common/boilerplate_amd_js  }}

import {getTinyMCE} from 'editor_tiny/loader';
import {getPluginMetadata} from 'editor_tiny/utils';

import {component, pluginName} from './common';
{{# self.defines_options }}
import {register as registerOptions} from './options';
{{/ self.defines_options }}
{{# self.defines_commands }}
import {getSetup as getCommandSetup} from './commands';
import * as Configuration from './configuration';
{{/ self.defines_commands }}

// Setup the {{ component }} Plugin.
export default new Promise(async(resolve) => {
    // Note: The PluginManager.add function does not support asynchronous configuration.
    // Perform any asynchronous configuration here, and then call the PluginManager.add function.
    const [
        tinyMCE,
        pluginMetadata,
{{# self.defines_commands }}
        setupCommands,
{{/ self.defines_commands }}
    ] = await Promise.all([
        getTinyMCE(),
        getPluginMetadata(component, pluginName),
{{# self.defines_commands }}
        getCommandSetup(),
{{/ self.defines_commands }}
    ]);

    // Reminder: Any asynchronous code must be run before this point.
    tinyMCE.PluginManager.add(pluginName, (editor) => {
{{# self.defines_options }}
        // Register any options that your plugin has
        registerOptions(editor);

{{/ self.defines_options }}
{{#  self.defines_commands }}
        // Setup any commands such as buttons, menu items, and so on.
        setupCommands(editor);

{{/ self.defines_commands }}
        // Return the pluginMetadata object. This is used by TinyMCE to display a help link for your plugin.
        return pluginMetadata;
    });

{{#  self.defines_commands }}
    resolve([pluginName, Configuration]);
{{/  self.defines_commands }}
{{^  self.defines_commands }}
    resolve(pluginName);
{{/  self.defines_commands }}
});
