import { Platform } from './platform';
import { windowLoad } from '../util/dom';
var win = window;
var doc = document;
var PLATFORM_CORE = {
    name: 'core',
    settings: {
        mode: 'md',
        keyboardHeight: 290
    }
};
var PLATFORM_MOBILE = {
    name: 'mobile'
};
var PLATFORM_PHABLET = {
    name: 'phablet',
    isMatch: function (p) {
        var smallest = Math.min(p.width(), p.height());
        var largest = Math.max(p.width(), p.height());
        return (smallest > 390 && smallest < 520) &&
            (largest > 620 && largest < 800);
    }
};
var PLATFORM_TABLET = {
    name: 'tablet',
    isMatch: function (p) {
        var smallest = Math.min(p.width(), p.height());
        var largest = Math.max(p.width(), p.height());
        return (smallest > 460 && smallest < 820) &&
            (largest > 780 && largest < 1400);
    }
};
var PLATFORM_ANDROID = {
    name: 'android',
    superset: 'mobile',
    subsets: [
        'phablet',
        'tablet'
    ],
    settings: {
        activator: function (p) {
            if (p.testNavigatorPlatform('linux')) {
                var chromeVersion = p.matchUserAgentVersion(/Chrome\/(\d+).(\d+)?/);
                if (chromeVersion) {
                    return (parseInt(chromeVersion.major, 10) < 36 ? 'none' : 'ripple');
                }
                if (p.version().major < 5) {
                    return 'none';
                }
            }
            return 'ripple';
        },
        autoFocusAssist: 'immediate',
        hoverCSS: false,
        keyboardHeight: 300,
        mode: 'md',
    },
    isMatch: function (p) {
        return p.isPlatformMatch('android', ['android', 'silk'], ['windows phone']);
    },
    versionParser: function (p) {
        return p.matchUserAgentVersion(/Android (\d+).(\d+)?/);
    }
};
var PLATFORM_IOS = {
    name: 'ios',
    superset: 'mobile',
    subsets: [
        'ipad',
        'iphone'
    ],
    settings: {
        autoFocusAssist: 'delay',
        hoverCSS: false,
        inputBlurring: isIOSDevice,
        inputCloning: isIOSDevice,
        keyboardHeight: 300,
        mode: 'ios',
        scrollAssist: isIOSDevice,
        statusbarPadding: !!(win.cordova),
        swipeBackEnabled: isIOSDevice,
        swipeBackThreshold: 40,
        tapPolyfill: isIOSDevice,
        virtualScrollEventAssist: !(win.indexedDB),
        canDisableScroll: !!(win.indexedDB),
    },
    isMatch: function (p) {
        return p.isPlatformMatch('ios', ['iphone', 'ipad', 'ipod'], ['windows phone']);
    },
    versionParser: function (p) {
        return p.matchUserAgentVersion(/OS (\d+)_(\d+)?/);
    }
};
var PLATFORM_IPAD = {
    name: 'ipad',
    superset: 'tablet',
    settings: {
        keyboardHeight: 500,
    },
    isMatch: function (p) {
        return p.isPlatformMatch('ipad');
    }
};
var PLATFORM_IPHONE = {
    name: 'iphone',
    subsets: [
        'phablet'
    ],
    isMatch: function (p) {
        return p.isPlatformMatch('iphone');
    }
};
var PLATFORM_WINDOWS = {
    name: 'windows',
    superset: 'mobile',
    subsets: [
        'phablet',
        'tablet'
    ],
    settings: {
        mode: 'wp',
        autoFocusAssist: 'immediate',
        hoverCSS: false
    },
    isMatch: function (p) {
        return p.isPlatformMatch('windows', ['windows phone']);
    },
    versionParser: function (p) {
        return p.matchUserAgentVersion(/Windows Phone (\d+).(\d+)?/);
    }
};
var PLATFORM_CORDOVA = {
    name: 'cordova',
    isEngine: true,
    initialize: function (p) {
        p.prepareReady = function () {
            windowLoad(function () {
                doc.addEventListener('deviceready', function () {
                    doc.addEventListener('backbutton', function (ev) {
                        p.zone.run(function () {
                            p.backButton.emit(ev);
                        });
                    });
                    doc.addEventListener('pause', function (ev) {
                        p.zone.run(function () {
                            p.pause.emit(ev);
                        });
                    });
                    doc.addEventListener('resume', function (ev) {
                        p.zone.run(function () {
                            p.resume.emit(ev);
                        });
                    });
                    p.exitApp = function () {
                        win.navigator.app.exitApp();
                    };
                    p.triggerReady('cordova');
                });
            });
        };
    },
    isMatch: function () {
        return !!(win.cordova || win.PhoneGap || win.phonegap);
    }
};
function isIOSDevice(p) {
    return p.testNavigatorPlatform('iphone|ipad|ipod');
}
export function setupPlatformRegistry() {
    Platform.register(PLATFORM_CORE);
    Platform.register(PLATFORM_MOBILE);
    Platform.register(PLATFORM_PHABLET);
    Platform.register(PLATFORM_TABLET);
    Platform.register(PLATFORM_ANDROID);
    Platform.register(PLATFORM_IOS);
    Platform.register(PLATFORM_IPAD);
    Platform.register(PLATFORM_IPHONE);
    Platform.register(PLATFORM_WINDOWS);
    Platform.register(PLATFORM_CORDOVA);
    Platform.setDefault('core');
}
