import { TEXTAREA_AUTO_HEIGHT } from '../directive-names';
var ajustHeight = function (element) {
    if (element.tagName === 'TEXTAREA') {
        var outerHeight_1 = parseInt(window.getComputedStyle(element).height, 10);
        var diff = outerHeight_1 - element.clientHeight;
        element.style.height = '0';
        if (element.scrollHeight + diff > 0) {
            element.style.height = element.scrollHeight + diff + 'px';
        }
        else {
            element.style.removeProperty('height');
        }
    }
};
export var MTextareaAutoHeight = {
    bind: function (element, binding, node) {
        element.disabledUpdate = false;
        var setDisabledUpdate = function () {
            element.disabledUpdate = true;
            setTimeout(function () {
                if (element.disabledUpdate) {
                    element.disabledUpdate = false;
                }
            }, 800);
        };
        if (node.context && element.tagName === 'TEXTAREA') {
            node.context.$nextTick(function () {
                if (node.context && element) {
                    element.ajustHeight = function () {
                        ajustHeight(element);
                        setDisabledUpdate();
                    };
                    element.setDisabledUpdate = function () {
                        setDisabledUpdate();
                    };
                    element.addEventListener('mousedown', element.setDisabledUpdate);
                    element.addEventListener('input', element.ajustHeight);
                    window.addEventListener('resize', element.ajustHeight);
                    setTimeout(function () {
                        element.ajustHeight();
                    });
                }
            });
        }
    },
    update: function (element) {
        if (!element.disabledUpdate) {
            ajustHeight(element);
        }
    },
    unbind: function (element, binding) {
        if (element.tagName === 'TEXTAREA') {
            if (element.setDisabledUpdate) {
                element.removeEventListener('mousedown', element.setDisabledUpdate);
            }
            element.removeEventListener('input', element.ajustHeight);
            window.removeEventListener('resize', element.ajustHeight);
        }
    }
};
var TextareaAutoHeightPlugin = {
    install: function (v, options) {
        v.directive(TEXTAREA_AUTO_HEIGHT, MTextareaAutoHeight);
    }
};
export default TextareaAutoHeightPlugin;
//# sourceMappingURL=textarea-auto-height.js.map