"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UITextarea = void 0;
var guid_1 = require("./../../resources/elements/utilities/guid");
var aurelia_framework_1 = require("aurelia-framework");
var base_input_1 = require("./base-input");
var input_wrapper_1 = require("./input-wrapper");
var UITextarea = exports.UITextarea = (function (_super) {
    __extends(UITextarea, _super);
    function UITextarea(element, bindingEngine) {
        var _this = _super.call(this, element) || this;
        _this.bindingEngine = bindingEngine;
        _this.number = null;
        _this.placeholder = "";
        _this.maxHeight = 168;
        _this.minHeight = 48;
        _this.resizable = false;
        _this.size = 'sm';
        _this.isValueObservable = true;
        _this.hiddenDivId = guid_1.Guid.newGuid().toString();
        return _this;
    }
    Object.defineProperty(UITextarea.prototype, "counter", {
        get: function () {
            if (this.maxlength) {
                return "".concat(this.value ? this.value.length : 0, " of ").concat(this.maxlength);
            }
            else {
                return "".concat(this.value ? this.value.length : 0);
            }
        },
        enumerable: false,
        configurable: true
    });
    UITextarea.prototype.attached = function () {
        this.textareaAutoResize();
    };
    UITextarea.prototype.valueChanged = function (newValue, oldValue) {
        if (this.isValueObservable) {
            this.textareaAutoResize();
        }
    };
    UITextarea.prototype.detached = function () {
        var _a;
        (_a = this.disposable) === null || _a === void 0 ? void 0 : _a.dispose();
        var hiddenDiv = $('#' + this.hiddenDivId).first();
        if (hiddenDiv != null) {
            hiddenDiv.detach();
        }
    };
    UITextarea.prototype.textareaAutoResize = function () {
        var $textarea = $(this.inputEl);
        if ($textarea.length == 0) {
            return;
        }
        var hiddenDiv = $('#' + this.hiddenDivId).first();
        if (!hiddenDiv.length) {
            hiddenDiv = $('<div id=' + this.hiddenDivId + ' class="hiddendiv common"></div>');
            $('body').append(hiddenDiv);
        }
        if (this.allowClear) {
            $textarea.css('padding-right', '22.5px');
        }
        var fontFamily = $textarea.css('font-family');
        var fontSize = $textarea.css('font-size');
        var fontWeight = $textarea.css('font-weight');
        var lineHeight = $textarea.css('line-height');
        var letterSpacing = $textarea.css('letter-spacing');
        var paddingTop = $textarea.css('padding-top');
        var paddingRight = $textarea.css('padding-right');
        var paddingBottom = $textarea.css('padding-bottom');
        var paddingLeft = $textarea.css('padding-left');
        if (fontFamily) {
            hiddenDiv.css('font-family', fontFamily);
        }
        if (fontSize) {
            hiddenDiv.css('font-size', fontSize);
        }
        if (fontWeight) {
            hiddenDiv.css('font-weight', fontWeight);
        }
        if (lineHeight) {
            hiddenDiv.css('line-height', lineHeight);
        }
        if (letterSpacing) {
            hiddenDiv.css('letter-spacing', letterSpacing);
        }
        if (paddingTop) {
            hiddenDiv.css('padding-top', paddingTop);
        }
        if (paddingRight) {
            hiddenDiv.css('padding-right', paddingRight);
        }
        if (paddingBottom) {
            hiddenDiv.css('padding-bottom', paddingBottom);
        }
        if (paddingLeft) {
            hiddenDiv.css('padding-left', paddingLeft);
        }
        if (!$textarea.data('original-height')) {
            $textarea.data('original-height', $textarea.height());
        }
        if ($textarea.attr('wrap') === 'off') {
            hiddenDiv.css('overflow-wrap', 'normal').css('white-space', 'pre');
        }
        hiddenDiv.text($textarea[0].value + '\n');
        var content = hiddenDiv.html().replace(/\n/g, '<br>');
        hiddenDiv.html(content);
        if ($textarea[0].offsetWidth > 0 && $textarea[0].offsetHeight > 0) {
            hiddenDiv.css('width', $textarea.width() + 'px');
        }
        else {
            hiddenDiv.css('width', window.innerWidth / 2 + 'px');
        }
        var height = this.minHeight;
        if ($textarea.data('original-height') <= hiddenDiv.innerHeight()) {
            height = hiddenDiv.innerHeight();
        }
        else if ($textarea[0].value.length < $textarea.data('previous-length')) {
            height = $textarea.data('original-height');
        }
        if (this.minHeight != null && this.minHeight > 0) {
            if (height < this.minHeight) {
                height = this.minHeight;
            }
        }
        if (this.maxHeight != null && this.maxHeight > 0) {
            if (this.allowClear) {
                var clearBtn = $(this.element).find('div.ui-input__clear').first();
                clearBtn.css('right', (height > this.maxHeight ? '18px' : '0'));
            }
            if (height > this.maxHeight) {
                height = this.maxHeight;
            }
        }
        $textarea.css('height', height + 'px');
        $textarea.data('previous-length', $textarea[0].value.length);
    };
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", String)
    ], UITextarea.prototype, "value", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)({ defaultBindingMode: aurelia_framework_1.bindingMode.twoWay }),
        __metadata("design:type", Number)
    ], UITextarea.prototype, "number", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UITextarea.prototype, "placeholder", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Number)
    ], UITextarea.prototype, "maxHeight", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Number)
    ], UITextarea.prototype, "minHeight", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", Boolean)
    ], UITextarea.prototype, "resizable", void 0);
    __decorate([
        (0, aurelia_framework_1.bindable)(),
        __metadata("design:type", String)
    ], UITextarea.prototype, "size", void 0);
    __decorate([
        (0, aurelia_framework_1.computedFrom)("value", "maxlength"),
        __metadata("design:type", Object),
        __metadata("design:paramtypes", [])
    ], UITextarea.prototype, "counter", null);
    UITextarea = __decorate([
        (0, aurelia_framework_1.customElement)("ui-textarea"),
        (0, aurelia_framework_1.viewResources)(input_wrapper_1.InputWrapper),
        __metadata("design:paramtypes", [Element, aurelia_framework_1.BindingEngine])
    ], UITextarea);
    return UITextarea;
}(base_input_1.BaseInput));

//# sourceMappingURL=ui-textarea.js.map
