"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VideoSpecs = exports.videoType = exports.videoNodeName = void 0;
const tslib_1 = require("tslib");
const log_1 = tslib_1.__importDefault(require("@diplodoc/transform/lib/log"));
const const_1 = require("./const.js");
const md_video_1 = require("./md-video.js");
const utils_1 = require("./utils.js");
// we don't support osf service
const availableServices = new Set([
    md_video_1.VideoService.YouTube,
    md_video_1.VideoService.Vimeo,
    md_video_1.VideoService.Vine,
    md_video_1.VideoService.Prezi,
]);
var const_2 = require("./const.js");
Object.defineProperty(exports, "videoNodeName", { enumerable: true, get: function () { return const_2.videoNodeName; } });
var utils_2 = require("./utils.js");
Object.defineProperty(exports, "videoType", { enumerable: true, get: function () { return utils_2.videoType; } });
const VideoSpecs = (builder, opts) => {
    const options = { ...md_video_1.defaults, ...opts, log: log_1.default };
    builder.configureMd((md) => md.use(md_video_1.videoPlugin, options));
    builder.addNode(const_1.videoNodeName, () => ({
        spec: {
            inline: true,
            atom: true,
            group: 'inline',
            attrs: { [const_1.VideoAttr.Service]: {}, [const_1.VideoAttr.VideoID]: {} },
            parseDOM: [
                {
                    tag: `span[${const_1.VideoAttr.Service}][${const_1.VideoAttr.VideoID}]`,
                    getAttrs(dom) {
                        const domElem = dom;
                        return {
                            [const_1.VideoAttr.Service]: domElem.getAttribute(const_1.VideoAttr.Service),
                            [const_1.VideoAttr.VideoID]: domElem.getAttribute(const_1.VideoAttr.VideoID),
                        };
                    },
                },
                {
                    tag: `iframe[${const_1.VideoAttr.Service}][${const_1.VideoAttr.VideoID}]`,
                    getAttrs(dom) {
                        const domElem = dom;
                        return {
                            [const_1.VideoAttr.Service]: domElem.getAttribute(const_1.VideoAttr.Service),
                            [const_1.VideoAttr.VideoID]: domElem.getAttribute(const_1.VideoAttr.VideoID),
                        };
                    },
                    priority: 100,
                },
            ],
            toDOM(node) {
                const service = node.attrs[const_1.VideoAttr.Service];
                const videoId = node.attrs[const_1.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: '',
                                [const_1.VideoAttr.Service]: service,
                                [const_1.VideoAttr.VideoID]: videoId,
                            },
                        ],
                    ];
                }
                return (0, utils_1.createViewStub)(node);
            },
        },
        fromMd: {
            tokenSpec: {
                name: const_1.videoNodeName,
                type: 'node',
                getAttrs: (tok) => ({
                    [const_1.VideoAttr.Service]: tok.service,
                    [const_1.VideoAttr.VideoID]: tok.videoID,
                }),
            },
        },
        toMd: (state, node) => {
            state.write((0, utils_1.serializeNodeToString)(node));
        },
    }));
};
exports.VideoSpecs = VideoSpecs;
//# sourceMappingURL=index.js.map
