import { getSupportedInputTypes, Platform } from '@angular/cdk/platform';
import { CommonModule } from '@angular/common';
import { Component, ViewEncapsulation, Input, Optional, Self, ElementRef, Directive, NgModule } from '@angular/core';
import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { NgControl } from '@angular/forms';
import { NtFormFieldControl } from '@ng-tangram/components/forms';

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;
};
let NtInputAddonComponent = class NtInputAddonComponent {
};
NtInputAddonComponent = __decorate([
    Component({
        selector: 'nt-input-addon, [nt-input-addon]',
        template: `<ng-content></ng-content>`,
        encapsulation: ViewEncapsulation.None,
        host: {
            'class': 'nt-input-addon input-group-label'
        }
    })
], NtInputAddonComponent);

var __decorate$1 = (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;
};
let NtInputGroupComponent = class NtInputGroupComponent {
    constructor() {
        this._transparent = false;
    }
    get transparent() { return this._transparent; }
    set transparent(value) {
        this._transparent = coerceBooleanProperty(value);
    }
};
__decorate$1([
    Input()
], NtInputGroupComponent.prototype, "transparent", null);
NtInputGroupComponent = __decorate$1([
    Component({
        selector: 'nt-input-group, [nt-input-group]',
        template: `<ng-content></ng-content>`,
        encapsulation: ViewEncapsulation.None,
        host: {
            'class': 'nt-input-group input-group',
            '[class.nt-input-group-transparent]': 'transparent'
        }
    })
], NtInputGroupComponent);

var __decorate$2 = (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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var NtInputDirective_1;
let NtInputDirective = NtInputDirective_1 = class NtInputDirective extends NtFormFieldControl {
    constructor(ngControl, _platform, _elementRef) {
        super();
        this.ngControl = ngControl;
        this._platform = _platform;
        this._elementRef = _elementRef;
        this._disabled = false;
        this._type = 'text';
        this._readonly = false;
        this._required = false;
        this._focused = false;
        this.placeholder = '';
    }
    get disabled() {
        if (this.ngControl && this.ngControl.disabled !== null) {
            return this.ngControl.disabled;
        }
        return this._disabled;
    }
    set disabled(value) {
        this._disabled = coerceBooleanProperty(value);
        if (this._focused) {
            this._focused = false;
        }
    }
    get required() { return this._required; }
    set required(value) { this._required = coerceBooleanProperty(value); }
    get type() { return this._type; }
    set type(value) {
        this._type = value || 'text';
        if (!this._isTextarea() && getSupportedInputTypes().has(this._type)) {
            this._elementRef.nativeElement.type = this._type;
        }
    }
    get value() { return this._elementRef.nativeElement.value; }
    set value(value) {
        if (value !== this.value) {
            this._elementRef.nativeElement.value = value;
        }
    }
    get readonly() { return this._readonly; }
    set readonly(value) { this._readonly = coerceBooleanProperty(value); }
    get empty() { return !this._elementRef.nativeElement.value && !this._isBadInput(); }
    focus() { this._elementRef.nativeElement.focus(); }
    _focusChanged(isFocused) {
        if (isFocused !== this._focused && !this.readonly) {
            this._focused = isFocused;
        }
    }
    _isBadInput() {
        const validity = this._elementRef.nativeElement.validity;
        return validity && validity.badInput;
    }
    _isTextarea() {
        const nativeElement = this._elementRef.nativeElement;
        const nodeName = this._platform.isBrowser ? nativeElement.nodeName : nativeElement.name;
        return nodeName ? nodeName.toLowerCase() === 'textarea' : false;
    }
};
NtInputDirective.ctorParameters = () => [
    { type: NgControl, decorators: [{ type: Optional }, { type: Self }] },
    { type: Platform },
    { type: ElementRef }
];
__decorate$2([
    Input()
], NtInputDirective.prototype, "placeholder", void 0);
__decorate$2([
    Input()
], NtInputDirective.prototype, "disabled", null);
__decorate$2([
    Input()
], NtInputDirective.prototype, "required", null);
__decorate$2([
    Input()
], NtInputDirective.prototype, "type", null);
__decorate$2([
    Input()
], NtInputDirective.prototype, "value", null);
__decorate$2([
    Input()
], NtInputDirective.prototype, "readonly", null);
NtInputDirective = NtInputDirective_1 = __decorate$2([
    Directive({
        selector: 'input[ntInput], textarea[ntInput]',
        host: {
            '(blur)': '_focusChanged(false)',
            '(focus)': '_focusChanged(true)',
        },
        providers: [
            { provide: NtFormFieldControl, useExisting: NtInputDirective_1 }
        ]
    }),
    __param(0, Optional()), __param(0, Self())
], NtInputDirective);

var __decorate$3 = (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;
};
let NtInputModule = class NtInputModule {
};
NtInputModule = __decorate$3([
    NgModule({
        imports: [CommonModule],
        exports: [NtInputDirective, NtInputGroupComponent, NtInputAddonComponent],
        declarations: [NtInputDirective, NtInputGroupComponent, NtInputAddonComponent],
        providers: [Platform]
    })
], NtInputModule);

export { NtInputAddonComponent, NtInputDirective, NtInputGroupComponent, NtInputModule };
//# sourceMappingURL=input.js.map
