import { Injectable, NgZone } from '@angular/core';
import { Config } from '../config/config';
import { DomController } from './dom-controller';
import { focusOutActiveElement, hasFocusedTextInput, nativeTimeout, clearNativeTimeout } from './dom';
import { Key } from './key';
export class Keyboard {
    constructor(config, _zone, _dom) {
        this._zone = _zone;
        this._dom = _dom;
        _zone.runOutsideAngular(() => {
            this.focusOutline(config.get('focusOutline'), document);
            window.addEventListener('native.keyboardhide', () => {
                clearNativeTimeout(this._tmr);
                this._tmr = nativeTimeout(() => {
                    if (hasFocusedTextInput()) {
                        focusOutActiveElement();
                    }
                }, 80);
            });
            window.addEventListener('native.keyboardshow', () => {
                clearNativeTimeout(this._tmr);
            });
        });
    }
    isOpen() {
        return hasFocusedTextInput();
    }
    onClose(callback, pollingInternval = KEYBOARD_CLOSE_POLLING, pollingChecksMax = KEYBOARD_POLLING_CHECKS_MAX) {
        (void 0);
        const self = this;
        let checks = 0;
        let promise = null;
        if (!callback) {
            promise = new Promise(resolve => { callback = resolve; });
        }
        function checkKeyboard() {
            (void 0);
            if (!self.isOpen() || checks > pollingChecksMax) {
                nativeTimeout(function () {
                    self._zone.run(function () {
                        (void 0);
                        callback();
                    });
                }, 400);
            }
            else {
                nativeTimeout(checkKeyboard, pollingInternval);
            }
            checks++;
        }
        nativeTimeout(checkKeyboard, pollingInternval);
        return promise;
    }
    close() {
        this._dom.read(() => {
            if (hasFocusedTextInput()) {
                (void 0);
                this._dom.write(() => {
                    focusOutActiveElement();
                });
            }
        });
    }
    focusOutline(setting, document) {
        const self = this;
        let isKeyInputEnabled = false;
        function cssClass() {
            this._dom.write(() => {
                document.body.classList[isKeyInputEnabled ? 'add' : 'remove']('focus-outline');
            });
        }
        if (setting === true) {
            isKeyInputEnabled = true;
            return cssClass();
        }
        else if (setting === false) {
            return;
        }
        function keyDown(ev) {
            if (!isKeyInputEnabled && ev.keyCode === Key.TAB) {
                isKeyInputEnabled = true;
                enableKeyInput();
            }
        }
        function pointerDown() {
            isKeyInputEnabled = false;
            enableKeyInput();
        }
        function enableKeyInput() {
            cssClass();
            self._zone.runOutsideAngular(() => {
                document.removeEventListener('mousedown', pointerDown);
                document.removeEventListener('touchstart', pointerDown);
                if (isKeyInputEnabled) {
                    document.addEventListener('mousedown', pointerDown);
                    document.addEventListener('touchstart', pointerDown);
                }
            });
        }
        document.addEventListener('keydown', keyDown);
    }
}
Keyboard.decorators = [
    { type: Injectable },
];
Keyboard.ctorParameters = [
    { type: Config, },
    { type: NgZone, },
    { type: DomController, },
];
const KEYBOARD_CLOSE_POLLING = 150;
const KEYBOARD_POLLING_CHECKS_MAX = 100;
//# sourceMappingURL=keyboard.js.map