"use strict";
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;
function HaikuDOMAdapter(bytecode, config, safeWindow) {
    if (!config) {
        config = {};
    }
    if (!config.options) {
        config.options = {};
    }
    if (!safeWindow) {
        if (typeof window !== 'undefined') {
            safeWindow = window;
        }
    }
    if (config.options.useWebkitPrefix === undefined) {
        if (safeWindow && safeWindow.document) {
            config.options.useWebkitPrefix = 'WebkitAppearance' in safeWindow.document.documentElement.style;
        }
    }
    return HaikuContext_1.default['createComponentFactory'](dom_1.default, bytecode, config, safeWindow);
}
exports.default = HaikuDOMAdapter;
HaikuDOMAdapter['defineOnWindow'] = function () {
    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 undefined;
                }
                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 undefined;
                }
                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']();
//# sourceMappingURL=HaikuDOMAdapter.js.map