"use strict";
/**
 * Copyright (c) Haiku 2016-2018. All rights reserved.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var HaikuContext_1 = require("./../../HaikuContext");
var dom_1 = require("./../../renderers/dom");
var pkg = require('./../../../package.json');
var VERSION = pkg.version;
/**
 * Example ways in which the export of this module is invoked:
 *
 * // via embed snippet
 * window.HaikuPlayer['2.0.125'](require('./code/main/code.js')) // #LEGACY
 * window.HaikuCore['2.0.125'](require('./code/main/code.js'))
 *
 * // via module require
 * var HaikuDOMAdapter = require('@haiku/core/dom')
 * module.exports = HaikuDOMAdapter(require('./code/main/code.js'))
 */
/**
 * @function HaikuDOMAdapter
 * @description Given a bytecode object, return a factory function which can create a DOM-playable component.
 */
// tslint:disable-next-line:variable-name
var HaikuDOMAdapter = function (bytecode, config, safeWindow) {
    if (!config) {
        // tslint:disable-next-line:no-parameter-reassignment
        config = {};
    }
    if (!safeWindow) {
        if (typeof window !== 'undefined') {
            // tslint:disable-next-line:no-parameter-reassignment
            safeWindow = window;
        }
    }
    if (config.useWebkitPrefix === undefined) {
        // Allow headless mode, e.g. in server-side rendering or in Node.js unit tests
        if (safeWindow && safeWindow.document) {
            config.useWebkitPrefix = 'WebkitAppearance' in safeWindow.document.documentElement.style;
        }
    }
    return HaikuContext_1.default.createComponentFactory(dom_1.default, bytecode, config, // Note: Full config object, of which options is one property!
    safeWindow);
};
HaikuDOMAdapter.defineOnWindow = function () {
    // Allow multiple instances of different versions to exist on the same page
    if (typeof window !== 'undefined') {
        if (!window.HaikuResolve) {
            var haikuResolutions_1 = {};
            window.HaikuResolve = function (playerVersion) {
                if (haikuResolutions_1[playerVersion]) {
                    return haikuResolutions_1[playerVersion];
                }
                var matches = playerVersion.match(/^(\d+)\.(\d+)\.(\d+)$/).map(Number);
                if (!matches) {
                    return;
                }
                var _ = matches[0], major = matches[1], minor = matches[2], patch = matches[3];
                var compatibleVersions = Object.keys(window.HaikuCore)
                    .map(function (semver) { return semver.split('.').map(Number); })
                    .filter(function (semverParts) {
                    if (semverParts.length !== 3 || semverParts[0] !== major) {
                        return false;
                    }
                    return semverParts[1] >= minor && ((semverParts[1] > minor) ? true : semverParts[2] >= patch);
                });
                if (compatibleVersions.length === 0) {
                    return;
                }
                compatibleVersions.sort(function (_a, _b) {
                    var __ = _a[0], aMinor = _a[1], aPatch = _a[2];
                    var ___ = _b[0], bMinor = _b[1], bPatch = _b[2];
                    if (aMinor < bMinor) {
                        return -1;
                    }
                    if (aMinor > bMinor) {
                        return 1;
                    }
                    return aPatch < bPatch ? -1 : 1;
                });
                return haikuResolutions_1[playerVersion] =
                    window.HaikuCore[compatibleVersions[compatibleVersions.length - 1].join('.')];
            };
        }
        if (!window.HaikuCore) {
            window.HaikuCore = {};
        }
        window.HaikuCore[VERSION] = HaikuDOMAdapter;
    }
};
HaikuDOMAdapter.defineOnWindow();
exports.default = HaikuDOMAdapter;
//# sourceMappingURL=HaikuDOMAdapter.js.map