import { log } from '@diplodoc/transform/lib/log.js';
import { VideoAttr, videoNodeName } from "./const.js";
import { VideoService, defaults, videoPlugin, } from "./md-video.js";
import { createViewStub, serializeNodeToString } from "./utils.js";
// we don't support osf service
const availableServices = new Set([
    VideoService.YouTube,
    VideoService.Vimeo,
    VideoService.Vine,
    VideoService.Prezi,
]);
export { videoNodeName } from "./const.js";
export { videoType } from "./utils.js";
export const VideoSpecs = (builder, opts) => {
    const options = { ...defaults, ...opts, log };
    builder.configureMd((md) => md.use(videoPlugin, options));
    builder.addNode(videoNodeName, () => ({
        spec: {
            inline: true,
            atom: true,
            group: 'inline',
            attrs: { [VideoAttr.Service]: {}, [VideoAttr.VideoID]: {} },
            parseDOM: [
                {
                    tag: `span[${VideoAttr.Service}][${VideoAttr.VideoID}]`,
                    getAttrs(dom) {
                        const domElem = dom;
                        return {
                            [VideoAttr.Service]: domElem.getAttribute(VideoAttr.Service),
                            [VideoAttr.VideoID]: domElem.getAttribute(VideoAttr.VideoID),
                        };
                    },
                },
                {
                    tag: `iframe[${VideoAttr.Service}][${VideoAttr.VideoID}]`,
                    getAttrs(dom) {
                        const domElem = dom;
                        return {
                            [VideoAttr.Service]: domElem.getAttribute(VideoAttr.Service),
                            [VideoAttr.VideoID]: domElem.getAttribute(VideoAttr.VideoID),
                        };
                    },
                    priority: 100,
                },
            ],
            toDOM(node) {
                const service = node.attrs[VideoAttr.Service];
                const videoId = node.attrs[VideoAttr.VideoID];
                if (availableServices.has(service) || !videoId) {
                    let src = '';
                    if (typeof options.videoUrl === 'function') {
                        src = options.videoUrl(service, videoId, options);
                    }
                    return [
                        'div',
                        {
                            class: 'embed-responsive embed-responsive-16by9',
                        },
                        [
                            'iframe',
                            {
                                class: `embed-responsive-item ${service}-player`,
                                type: 'text/html',
                                width: String(options[service].width),
                                height: String(options[service].height),
                                src: src,
                                frameborder: '0',
                                webkitallowfullscreen: '',
                                mozallowfullscreen: '',
                                allowfullscreen: '',
                                [VideoAttr.Service]: service,
                                [VideoAttr.VideoID]: videoId,
                            },
                        ],
                    ];
                }
                return createViewStub(node);
            },
        },
        fromMd: {
            tokenSpec: {
                name: videoNodeName,
                type: 'node',
                getAttrs: (tok) => ({
                    [VideoAttr.Service]: tok.service,
                    [VideoAttr.VideoID]: tok.videoID,
                }),
            },
        },
        toMd: (state, node) => {
            state.write(serializeNodeToString(node));
        },
    }));
};
//# sourceMappingURL=index.js.map
