import Browser from '../util/Browser.js';
import Command from './Command.js';
import Media from '../util/Media.js';

/**
 * Media Command
 */
export default class MediaCommand extends Command {
    /**
     * @inheritDoc
     */
    constructor(editor, tagName) {
        super(editor, tagName);

        let type;

        if (!this.tag || this.tag.group !== 'media' || !(type = Media.fromElement(this.tag.name))) {
            throw 'Invalid media element';
        }

        /**
         * Type
         *
         * @type {MediaTypeElement}
         * @readonly
         */
        this.type = type;
    }

    /**
     * @inheritDoc
     */
    execute() {
        const browser = this.editor.config.media[this.type.id];
        let url;

        if (browser) {
            Browser.open(this.editor.window, browser, data => this.insert(data));
        } else if (url = this.editor.window.prompt('URL')) {
            this.insert({src: url});
        }
    }

    /**
     * @inheritDoc
     */
    insert(data = {}) {
        if (!data.src) {
            return;
        }

        const a = this.editor.document.createElement('a');
        const origin = this.editor.window.origin || this.editor.window.location.origin;
        const call = async () => await Media.fromUrl(data.src);
        let type;

        a.href = data.src;
        data.src = a.origin === origin ? a.pathname : a.href;

        if (data.type && data.type === this.type.id || (type = call()) && type.id === this.type.id || !type && a.origin !== origin) {
            const figure = this.editor.document.createElement('figure');
            const media = this.editor.document.createElement(this.type.element);
            const figcaption = this.editor.document.createElement('figcaption');

            figure.classList.add(this.type.id);
            figure.appendChild(media);
            this.tag.attributes.forEach(item => {
                if (['allowfullscreen', 'controls'].includes(item)) {
                    media.setAttribute(item, item);
                } else if (data[item]) {
                    media.setAttribute(item, data[item]);
                }
            });
            figcaption.innerHTML = data.caption || '';
            figure.appendChild(figcaption);

            this.editor.insert(figure);
        }
    }
}

@@ -997,16 +997,22 @@
  */%0A    
+async 
 insert(d
@@ -1019,24 +1019,24 @@
 ata = %7B%7D) %7B%0A
-
         if (
@@ -1230,72 +1230,8 @@
 in;%0A
-        const call = async () =%3E await Media.fromUrl(data.src);%0A
     
@@ -1402,13 +1402,36 @@
 e = 
-call(
+await Media.fromUrl(data.src
 )) &

Fix regression