import outputEmbed from './outputEmbed';
import ArenaEmbed from './ArenaEmbed';
import ArenaEmbedSimple from './ArenaEmbedSimple';
import ArenaEmbedForm from './ArenaEmbedForm';
import ArenaEmbedYoutube from './ArenaEmbedYoutube';
const defaultOptions = {
    name: 'embed',
    title: 'Embed',
    icon: `<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="0 0 511.997 511.997" style="enable-background:new 0 0 511.997 511.997;" xml:space="preserve">
  <g>
    <g>
      <path d="M506.76,242.828l-118.4-125.44c-7.277-7.718-19.424-8.07-27.142-0.787c-7.706,7.277-8.064,19.43-0.781,27.142
        l105.965,112.256L360.437,368.268c-7.283,7.712-6.925,19.859,0.781,27.142c3.712,3.501,8.454,5.235,13.178,5.235
        c5.101,0,10.195-2.022,13.965-6.01l118.4-125.446C513.742,261.785,513.742,250.226,506.76,242.828z" fill="currentColor"/>
    </g>
  </g>
  <g>
    <g>
      <path d="M151.566,368.262L45.608,255.999l105.958-112.262c7.277-7.712,6.925-19.866-0.787-27.142
        c-7.706-7.277-19.866-6.925-27.142,0.787l-118.4,125.44c-6.982,7.398-6.982,18.963,0,26.362L123.643,394.63
        c3.776,4,8.864,6.016,13.965,6.016c4.723,0,9.466-1.741,13.171-5.242C158.498,388.127,158.843,375.974,151.566,368.262z" fill="currentColor"/>
    </g>
  </g>
  <g>
    <g>
      <path d="M287.061,52.697c-10.477-1.587-20.282,5.606-21.882,16.083l-56.32,368.64c-1.6,10.483,5.6,20.282,16.083,21.882
        c0.986,0.147,1.958,0.218,2.925,0.218c9.325,0,17.504-6.803,18.957-16.301l56.32-368.64
        C304.744,64.095,297.544,54.297,287.061,52.697z" fill="currentColor"/>
    </g>
  </svg>`,
    tag: 'ARENA-EMBED',
    attributes: {
        contenteditable: false,
    },
    allowedAttributes: ['href', 'type', 'postid', 'border'],
    shortcut: 'Alt + KeyE',
    hint: 'e',
    command: 'add-embed',
    components: [
        {
            component: 'arena-embed',
            componentConstructor: ArenaEmbed,
        },
        {
            component: 'arena-embed-simple',
            componentConstructor: ArenaEmbedSimple,
        },
        {
            component: 'arena-embed-form',
            componentConstructor: ArenaEmbedForm,
        },
        {
            component: 'arena-embed-youtube',
            componentConstructor: ArenaEmbedYoutube,
        },
    ],
    marks: [
        {
            tag: 'ARENA-EMBED',
            attributes: [],
        },
    ],
    output: outputEmbed,
};
const embedPlugin = (opts) => ({
    register: (ta) => {
        const { name, icon, title, tag, attributes, allowedAttributes, shortcut, hint, command, components, marks, output, } = Object.assign(Object.assign({}, defaultOptions), (opts || {}));
        if (components) {
            components.forEach(({ component, componentConstructor }) => {
                if (component && componentConstructor && !customElements.get(component)) {
                    customElements.define(component, componentConstructor);
                }
            });
        }
        const arena = ta.registerArena({
            name,
            tag,
            attributes,
            allowedAttributes,
            single: true,
            output,
        }, marks, [ta.getRootArenaName()]);
        if (command) {
            ta.registerCommand(command, (someTa, selection) => {
                const sel = someTa.insertBeforeSelected(selection, arena);
                return sel;
            });
            if (shortcut) {
                ta.registerShortcut(shortcut, command);
            }
            ta.registerCreator({
                name,
                icon,
                title,
                shortcut,
                hint,
                command,
                canShow: (node) => ta.isAllowedNode(node, arena),
            });
        }
        ta.registerArena({
            name: 'simple-embed',
            tag: 'ARENA-EMBED-SIMPLE',
            attributes: { contenteditable: false },
            allowedAttributes,
            single: true,
        }, [
            {
                tag: 'ARENA-EMBED-SIMPLE',
                attributes: [],
            },
        ], [ta.getRootArenaName()]);
        ta.subscribe('rendered', () => {
            setTimeout(() => {
                if (typeof window !== 'undefined' && window.twttr) {
                    const items = document.querySelectorAll('.twitter-tweet');
                    items.forEach((el) => {
                        var _a;
                        const id = el.getAttribute('postid');
                        const requested = el.getAttribute('requested');
                        if (id && !requested) {
                            el.setAttribute('requested', 'true');
                            (_a = window.twttr) === null || _a === void 0 ? void 0 : _a.widgets.createTweet(id, el);
                        }
                    });
                }
                if (typeof window !== 'undefined' && window.FB) {
                    window.FB.init({
                        xfbml: true,
                        version: 'v10.0',
                    });
                }
                if (typeof window !== 'undefined' && window.instgrm) {
                    window.instgrm.Embeds.process();
                }
            }, 100);
        });
    },
});
export default embedPlugin;
//# sourceMappingURL=embedPlugin.js.map