var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
(function (factory) {
    if (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); if (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", '../../util/dom', '../ion', '../../util/util'], factory);
    }
})(function (require, exports) {
    "use strict";
    var dom_1 = require('../../util/dom');
    var ion_1 = require('../ion');
    var util_1 = require('../../util/util');
    var InputBase = (function (_super) {
        __extends(InputBase, _super);
        function InputBase(config, _form, _item, _app, _platform, elementRef, renderer, _content, nav, ngControl, _dom) {
            var _this = this;
            _super.call(this, config, elementRef, renderer, 'input');
            this._form = _form;
            this._item = _item;
            this._app = _app;
            this._platform = _platform;
            this._content = _content;
            this._dom = _dom;
            this._disabled = false;
            this._type = 'text';
            this._value = '';
            this._nav = nav;
            this._useAssist = config.getBoolean('scrollAssist', false);
            this._usePadding = config.getBoolean('scrollPadding', this._useAssist);
            this._keyboardHeight = config.getNumber('keyboardHeight');
            this._autoFocusAssist = config.get('autoFocusAssist', 'delay');
            this._autoComplete = config.get('autocomplete', 'off');
            this._autoCorrect = config.get('autocorrect', 'off');
            if (ngControl) {
                ngControl.valueAccessor = this;
                this.inputControl = ngControl;
            }
            _form.register(this);
            this._scrollStart = _content.ionScrollStart.subscribe(function (ev) {
                _this.scrollHideFocus(ev, true);
            });
            this._scrollEnd = _content.ionScrollEnd.subscribe(function (ev) {
                _this.scrollHideFocus(ev, false);
            });
        }
        InputBase.prototype.scrollHideFocus = function (ev, shouldHideFocus) {
            var _this = this;
            if (!this._nav)
                return;
            if (this.hasFocus()) {
                this._dom.write(function () {
                    _this._native.hideFocus(shouldHideFocus);
                });
            }
        };
        InputBase.prototype.setItemInputControlCss = function () {
            var item = this._item;
            var nativeInput = this._native;
            var inputControl = this.inputControl;
            if (item && inputControl) {
                this.setControlCss(item, inputControl);
            }
            if (nativeInput && inputControl) {
                this.setControlCss(nativeInput, inputControl);
            }
        };
        InputBase.prototype.setControlCss = function (element, control) {
            element.setElementClass('ng-untouched', control.untouched);
            element.setElementClass('ng-touched', control.touched);
            element.setElementClass('ng-pristine', control.pristine);
            element.setElementClass('ng-dirty', control.dirty);
            element.setElementClass('ng-valid', control.valid);
            element.setElementClass('ng-invalid', !control.valid);
        };
        InputBase.prototype.setValue = function (val) {
            this._value = val;
            this.checkHasValue(val);
        };
        InputBase.prototype.setType = function (val) {
            this._type = 'text';
            if (val) {
                val = val.toLowerCase();
                if (/password|email|number|search|tel|url|date|month|time|week/.test(val)) {
                    this._type = val;
                }
            }
        };
        InputBase.prototype.setDisabled = function (val) {
            this._disabled = util_1.isTrueProperty(val);
            this._item && this._item.setElementClass('item-input-disabled', this._disabled);
            this._native && this._native.isDisabled(this._disabled);
        };
        InputBase.prototype.setClearOnEdit = function (val) {
            this._clearOnEdit = util_1.isTrueProperty(val);
        };
        InputBase.prototype.checkClearOnEdit = function (inputValue) {
            if (!this._clearOnEdit) {
                return;
            }
            if (this._didBlurAfterEdit && this.hasValue()) {
                this.clearTextInput();
            }
            this._didBlurAfterEdit = false;
        };
        InputBase.prototype.clearTextInput = function () { };
        InputBase.prototype.setNativeInput = function (nativeInput) {
            var _this = this;
            this._native = nativeInput;
            if (this._item && this._item.labelId !== null) {
                nativeInput.labelledBy(this._item.labelId);
            }
            nativeInput.valueChange.subscribe(function (inputValue) {
                _this.onChange(inputValue);
            });
            nativeInput.keydown.subscribe(function (inputValue) {
                _this.onKeydown(inputValue);
            });
            this.focusChange(this.hasFocus());
            nativeInput.focusChange.subscribe(function (textInputHasFocus) {
                _this.focusChange(textInputHasFocus);
                _this.checkHasValue(nativeInput.getValue());
                if (!textInputHasFocus) {
                    _this.onTouched(textInputHasFocus);
                }
            });
            this.checkHasValue(nativeInput.getValue());
            this.setDisabled(this._disabled);
            var ionInputEle = this._elementRef.nativeElement;
            var nativeInputEle = nativeInput.element();
            dom_1.copyInputAttributes(ionInputEle, nativeInputEle);
            if (ionInputEle.hasAttribute('autofocus')) {
                ionInputEle.removeAttribute('autofocus');
                if (this._autoFocusAssist === 'immediate') {
                    nativeInputEle.focus();
                }
                else if (this._autoFocusAssist === 'delay') {
                    setTimeout(function () {
                        nativeInputEle.focus();
                    }, 650);
                }
            }
            if (ionInputEle.hasAttribute('autocomplete')) {
                this._autoComplete = ionInputEle.getAttribute('autocomplete');
            }
            nativeInputEle.setAttribute('autocomplete', this._autoComplete);
            if (ionInputEle.hasAttribute('autocorrect')) {
                this._autoCorrect = ionInputEle.getAttribute('autocorrect');
            }
            nativeInputEle.setAttribute('autocorrect', this._autoCorrect);
        };
        InputBase.prototype.setNextInput = function (nextInput) {
            var _this = this;
            if (nextInput) {
                nextInput.focused.subscribe(function () {
                    _this._form.tabFocus(_this);
                });
            }
        };
        InputBase.prototype.writeValue = function (val) {
            this._value = val;
            this.checkHasValue(val);
        };
        InputBase.prototype.onChange = function (val) {
            this.checkHasValue(val);
        };
        InputBase.prototype.onKeydown = function (val) {
            if (this._clearOnEdit) {
                this.checkClearOnEdit(val);
            }
        };
        InputBase.prototype.onTouched = function (val) { };
        InputBase.prototype.hasFocus = function () {
            return this._native.hasFocus();
        };
        InputBase.prototype.hasValue = function () {
            var inputValue = this._value;
            return (inputValue !== null && inputValue !== undefined && inputValue !== '');
        };
        InputBase.prototype.checkHasValue = function (inputValue) {
            if (this._item) {
                var hasValue = (inputValue !== null && inputValue !== undefined && inputValue !== '');
                this._item.setElementClass('input-has-value', hasValue);
            }
        };
        InputBase.prototype.focusChange = function (inputHasFocus) {
            if (this._item) {
                (void 0);
                this._item.setElementClass('input-has-focus', inputHasFocus);
            }
            if (this._clearOnEdit && !inputHasFocus && this.hasValue()) {
                this._didBlurAfterEdit = true;
            }
        };
        InputBase.prototype.pointerStart = function (ev) {
            if (ev.type === 'touchstart') {
                this._isTouch = true;
            }
            if ((this._isTouch || (!this._isTouch && ev.type === 'mousedown')) && this._app.isEnabled()) {
                this._coord = dom_1.pointerCoord(ev);
            }
            (void 0);
        };
        InputBase.prototype.pointerEnd = function (ev) {
            (void 0);
            if ((this._isTouch && ev.type === 'mouseup') || !this._app.isEnabled()) {
                ev.preventDefault();
                ev.stopPropagation();
            }
            else if (this._coord) {
                var endCoord = dom_1.pointerCoord(ev);
                if (!dom_1.hasPointerMoved(8, this._coord, endCoord) && !this.hasFocus()) {
                    ev.preventDefault();
                    ev.stopPropagation();
                    this.initFocus();
                }
            }
            this._coord = null;
        };
        InputBase.prototype.initFocus = function () {
            var _this = this;
            var content = this._content;
            (void 0);
            if (content) {
                var ele = this._elementRef.nativeElement;
                ele = ele.closest('ion-item,[ion-item]') || ele;
                var scrollData = getScrollData(ele.offsetTop, ele.offsetHeight, content.getContentDimensions(), this._keyboardHeight, this._platform.height());
                if (Math.abs(scrollData.scrollAmount) < 4) {
                    this.setFocus();
                    this._app.setEnabled(true);
                    this._nav && this._nav.setTransitioning(false);
                    if (this._usePadding) {
                        content.clearScrollPaddingFocusOut();
                    }
                    return;
                }
                if (this._usePadding) {
                    content.addScrollPadding(scrollData.scrollPadding);
                }
                var scrollDuration = getScrollAssistDuration(scrollData.scrollAmount);
                this._app.setEnabled(false, scrollDuration);
                this._nav && this._nav.setTransitioning(true);
                this._native.beginFocus(true, scrollData.inputSafeY);
                content.scrollTo(0, scrollData.scrollTo, scrollDuration, function () {
                    (void 0);
                    _this._native.beginFocus(false, 0);
                    _this.setFocus();
                    _this._app.setEnabled(true);
                    _this._nav && _this._nav.setTransitioning(false);
                    if (_this._usePadding) {
                        content.clearScrollPaddingFocusOut();
                    }
                });
            }
            else {
                this.setFocus();
            }
        };
        InputBase.prototype.setFocus = function () {
            this._form.setAsFocused(this);
            (void 0);
            this._native.setFocus();
            document.body.scrollTop = 0;
        };
        InputBase.prototype.registerOnChange = function (fn) { this.onChange = fn; };
        InputBase.prototype.registerOnTouched = function (fn) { this.onTouched = fn; };
        InputBase.prototype.focusNext = function () {
            this._form.tabFocus(this);
        };
        return InputBase;
    }(ion_1.Ion));
    exports.InputBase = InputBase;
    function getScrollData(inputOffsetTop, inputOffsetHeight, scrollViewDimensions, keyboardHeight, plaformHeight) {
        var inputTop = (inputOffsetTop + scrollViewDimensions.contentTop - scrollViewDimensions.scrollTop);
        var inputBottom = (inputTop + inputOffsetHeight);
        var safeAreaTop = scrollViewDimensions.contentTop;
        var safeAreaHeight = (plaformHeight - keyboardHeight - safeAreaTop) / 2;
        var safeAreaBottom = safeAreaTop + safeAreaHeight;
        var inputTopWithinSafeArea = (inputTop >= safeAreaTop && inputTop <= safeAreaBottom);
        var inputTopAboveSafeArea = (inputTop < safeAreaTop);
        var inputTopBelowSafeArea = (inputTop > safeAreaBottom);
        var inputBottomWithinSafeArea = (inputBottom >= safeAreaTop && inputBottom <= safeAreaBottom);
        var inputBottomBelowSafeArea = (inputBottom > safeAreaBottom);
        var scrollData = {
            scrollAmount: 0,
            scrollTo: 0,
            scrollPadding: 0,
            inputSafeY: 0
        };
        if (inputTopWithinSafeArea && inputBottomWithinSafeArea) {
            return scrollData;
        }
        if (inputTopBelowSafeArea || inputBottomBelowSafeArea || inputTopAboveSafeArea) {
            if (safeAreaHeight > inputOffsetHeight) {
                scrollData.scrollAmount = Math.round(safeAreaBottom - inputBottom);
            }
            else {
                scrollData.scrollAmount = Math.round(safeAreaTop - inputTop);
            }
            scrollData.inputSafeY = -(inputTop - safeAreaTop) + 4;
            if (inputTopAboveSafeArea && scrollData.scrollAmount > inputOffsetHeight) {
                scrollData.scrollAmount = inputOffsetHeight;
            }
        }
        scrollData.scrollTo = (scrollViewDimensions.scrollTop - scrollData.scrollAmount);
        scrollData.scrollPadding = keyboardHeight;
        return scrollData;
    }
    exports.getScrollData = getScrollData;
    var SCROLL_ASSIST_SPEED = 0.3;
    function getScrollAssistDuration(distanceToScroll) {
        distanceToScroll = Math.abs(distanceToScroll);
        var duration = distanceToScroll / SCROLL_ASSIST_SPEED;
        return Math.min(400, Math.max(150, duration));
    }
});
//# sourceMappingURL=input-base.js.map