import { coerceBooleanProperty } from '@angular/cdk/coercion';
import { EventEmitter, ChangeDetectorRef, Attribute, Self, Optional, Input, Output, Component, ViewEncapsulation, ChangeDetectionStrategy, NgModule } from '@angular/core';
import { NgControl, NG_VALUE_ACCESSOR } from '@angular/forms';
import { CommonModule } from '@angular/common';

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 __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
var NtSwitchComponent_1;
let uniqueId = 0;
class NtSwitchChange {
    constructor(source, checked) {
        this.source = source;
        this.checked = checked;
    }
}
let NtSwitchComponent = NtSwitchComponent_1 = class NtSwitchComponent {
    constructor(_changeDetectorRef, tabIndex, ngControl) {
        this._changeDetectorRef = _changeDetectorRef;
        this.ngControl = ngControl;
        this.id = `nt-switch-${uniqueId++}`;
        this._disabled = false;
        this._checked = false;
        this._circle = false;
        this.change = new EventEmitter();
        this._onChange = () => { };
        this._onTouched = () => { };
        if (this.ngControl) {
            this.ngControl.valueAccessor = this;
        }
        this.tabIndex = parseInt(tabIndex) || 0;
    }
    get disabled() { return this._disabled; }
    set disabled(value) {
        if (value !== this.disabled) {
            this._disabled = coerceBooleanProperty(value);
            this._changeDetectorRef.markForCheck();
        }
    }
    get checked() { return this._checked; }
    set checked(value) {
        if (value !== this.checked) {
            this._checked = coerceBooleanProperty(value);
            this._changeDetectorRef.markForCheck();
        }
    }
    get circle() { return this._circle; }
    set circle(value) {
        if (value !== this.circle) {
            this._circle = coerceBooleanProperty(value);
        }
    }
    writeValue(value) {
        this._checked = !!value;
    }
    registerOnChange(fn) {
        this._onChange = fn;
    }
    registerOnTouched(fn) {
        this._onTouched = fn;
    }
    setDisabledState(isDisabled) {
        this.disabled = isDisabled;
    }
    _onInputClick(event) {
        event.stopPropagation();
        if (!this.disabled) {
            this._checked = !this._checked;
            this._onTouched();
            this._emitChangeEvent();
        }
    }
    _onInteractionEvent(event) {
        event.stopPropagation();
    }
    _emitChangeEvent() {
        const event = new NtSwitchChange(this, this.checked);
        this._onChange(this.checked);
        this.change.emit(event);
    }
};
NtSwitchComponent.ctorParameters = () => [
    { type: ChangeDetectorRef },
    { type: String, decorators: [{ type: Attribute, args: ['tabindex',] }] },
    { type: NgControl, decorators: [{ type: Self }, { type: Optional }] }
];
__decorate([
    Input()
], NtSwitchComponent.prototype, "disabled", null);
__decorate([
    Input()
], NtSwitchComponent.prototype, "checked", null);
__decorate([
    Input()
], NtSwitchComponent.prototype, "circle", null);
__decorate([
    Output()
], NtSwitchComponent.prototype, "change", void 0);
NtSwitchComponent = NtSwitchComponent_1 = __decorate([
    Component({
        selector: 'nt-switch',
        template: "<input class=\"switch-input\" type=\"checkbox\"\n  [attr.id]=\"id\"\n  [tabIndex]=\"tabIndex\"\n  [checked]=\"checked\"\n  [disabled]=\"disabled\"\n  (change)=\"_onInteractionEvent($event)\"\n  (click)=\"_onInputClick($event)\">\n<label class=\"switch-paddle\" [attr.for]=\"id\"></label>\n",
        encapsulation: ViewEncapsulation.None,
        changeDetection: ChangeDetectionStrategy.OnPush,
        host: {
            'class': 'switch nt-switch',
            '[class.nt-switch-checked]': 'checked',
            '[class.nt-switch-disabled]': 'disabled',
            '[class.nt-switch-circle]': 'circle'
        },
        providers: [
            { provide: NG_VALUE_ACCESSOR, useExisting: NtSwitchComponent_1, multi: true }
        ]
    }),
    __param(1, Attribute('tabindex')),
    __param(2, Self()), __param(2, Optional())
], NtSwitchComponent);

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 NtSwitchModule = class NtSwitchModule {
};
NtSwitchModule = __decorate$1([
    NgModule({
        imports: [CommonModule],
        exports: [NtSwitchComponent],
        declarations: [NtSwitchComponent]
    })
], NtSwitchModule);

export { NtSwitchChange, NtSwitchComponent, NtSwitchModule };
//# sourceMappingURL=switch.js.map
