(function (global, factory) {
    typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/cdk/coercion'), require('@angular/core'), require('@angular/forms'), require('rxjs'), require('rxjs/operators'), require('@ng-tangram/components/forms'), require('@angular/common')) :
    typeof define === 'function' && define.amd ? define('@ng-tangram/components/checkbox', ['exports', '@angular/cdk/coercion', '@angular/core', '@angular/forms', 'rxjs', 'rxjs/operators', '@ng-tangram/components/forms', '@angular/common'], factory) :
    (global = global || self, factory((global.nt = global.nt || {}, global.nt.components = global.nt.components || {}, global.nt.components.checkbox = {}), global.ng.cdk.coercion, global.ng.core, global.ng.forms, global.rxjs, global.rxjs.operators, global.nt.components.forms, global.ng.common));
}(this, (function (exports, coercion, core, forms, rxjs, operators, forms$1, common) { 'use strict';

    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 uniqueId = 0;
    var NtCheckboxChange = (function () {
        function NtCheckboxChange(source, checked) {
            this.source = source;
            this.checked = checked;
        }
        return NtCheckboxChange;
    }());
    var NtCheckboxComponent = (function () {
        function NtCheckboxComponent(_changeDetectorRef, tabIndex, ngControl) {
            this._changeDetectorRef = _changeDetectorRef;
            this.ngControl = ngControl;
            this.id = "nt-checkbox-" + uniqueId++;
            this._disabled = false;
            this._readonly = false;
            this._checked = false;
            this._indeterminate = false;
            this.change = new core.EventEmitter();
            this.indeterminateChange = new core.EventEmitter();
            this._onChange = function () { };
            this._onTouched = function () { };
            if (this.ngControl) {
                this.ngControl.valueAccessor = this;
            }
            this.tabIndex = parseInt(tabIndex) || 0;
        }
        Object.defineProperty(NtCheckboxComponent.prototype, "value", {
            get: function () { return this._value; },
            set: function (value) { this._value = value; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxComponent.prototype, "disabled", {
            get: function () { return this._disabled; },
            set: function (value) {
                if (value !== this.disabled) {
                    this._disabled = coercion.coerceBooleanProperty(value);
                    this._changeDetectorRef.markForCheck();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxComponent.prototype, "readonly", {
            get: function () { return this._readonly; },
            set: function (value) { this._readonly = coercion.coerceBooleanProperty(value); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxComponent.prototype, "checked", {
            get: function () { return this._checked; },
            set: function (value) {
                if (value !== this.checked) {
                    this._checked = coercion.coerceBooleanProperty(value);
                    this._changeDetectorRef.markForCheck();
                }
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxComponent.prototype, "indeterminate", {
            get: function () { return this._indeterminate; },
            set: function (value) {
                var changed = value !== this._indeterminate;
                this._indeterminate = value;
                if (changed) {
                    this.indeterminateChange.emit(this._indeterminate);
                }
            },
            enumerable: true,
            configurable: true
        });
        NtCheckboxComponent.prototype.writeValue = function (value) {
            this._checked = !!value;
        };
        NtCheckboxComponent.prototype.registerOnChange = function (fn) {
            this._onChange = fn;
        };
        NtCheckboxComponent.prototype.registerOnTouched = function (fn) {
            this._onTouched = fn;
        };
        NtCheckboxComponent.prototype.setDisabledState = function (isDisabled) {
            this.disabled = isDisabled;
        };
        NtCheckboxComponent.prototype._onInputClick = function (event) {
            event.stopPropagation();
            if (!this.disabled) {
                this._checked = !this._checked;
                this._onTouched();
                this._emitChangeEvent();
            }
        };
        NtCheckboxComponent.prototype._onInteractionEvent = function (event) {
            event.stopPropagation();
        };
        NtCheckboxComponent.prototype._emitChangeEvent = function () {
            var event = new NtCheckboxChange(this, this.checked);
            this._onChange(this.checked);
            this.change.emit(event);
        };
        NtCheckboxComponent.ctorParameters = function () { return [
            { type: core.ChangeDetectorRef },
            { type: String, decorators: [{ type: core.Attribute, args: ['tabindex',] }] },
            { type: forms.NgControl, decorators: [{ type: core.Self }, { type: core.Optional }] }
        ]; };
        __decorate([
            core.Input()
        ], NtCheckboxComponent.prototype, "value", null);
        __decorate([
            core.Input()
        ], NtCheckboxComponent.prototype, "disabled", null);
        __decorate([
            core.Input()
        ], NtCheckboxComponent.prototype, "readonly", null);
        __decorate([
            core.Input()
        ], NtCheckboxComponent.prototype, "checked", null);
        __decorate([
            core.Input()
        ], NtCheckboxComponent.prototype, "indeterminate", null);
        __decorate([
            core.Output()
        ], NtCheckboxComponent.prototype, "change", void 0);
        __decorate([
            core.Output()
        ], NtCheckboxComponent.prototype, "indeterminateChange", void 0);
        NtCheckboxComponent = __decorate([
            core.Component({
                selector: 'nt-checkbox',
                encapsulation: core.ViewEncapsulation.None,
                changeDetection: core.ChangeDetectionStrategy.OnPush,
                template: "<label class=\"nt-checkbox-layout\">\n  <div class=\"nt-checkbox-input-container\">\n    <input class=\"cdk-visually-hidden\"\n      type=\"checkbox\"\n      [tabIndex]=\"tabIndex\"\n      [checked]=\"checked\"\n      [disabled]=\"disabled\"\n      [readonly]=\"readonly\"\n      (change)=\"_onInteractionEvent($event)\"\n      (click)=\"_onInputClick($event)\">\n    <div class=\"nt-checkbox-input nt-form-control\"></div>\n  </div>\n  <span class=\"nt-checkbox-label\">\n    <ng-content></ng-content>\n  </span>\n</label>\n",
                host: {
                    'class': 'nt-checkbox',
                    '[class.nt-checkbox-checked]': 'checked',
                    '[class.nt-checkbox-disabled]': 'disabled',
                    '[class.nt-checkbox-indeterminate]': 'indeterminate',
                }
            }),
            __param(1, core.Attribute('tabindex')),
            __param(2, core.Self()), __param(2, core.Optional())
        ], NtCheckboxComponent);
        return NtCheckboxComponent;
    }());

    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 (b.hasOwnProperty(p)) d[p] = b[p]; };
            return extendStatics(d, b);
        };
        return function (d, b) {
            extendStatics(d, b);
            function __() { this.constructor = d; }
            d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
        };
    })();
    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;
    };
    var __param$1 = (this && this.__param) || function (paramIndex, decorator) {
        return function (target, key) { decorator(target, key, paramIndex); }
    };
    var __read = (this && this.__read) || function (o, n) {
        var m = typeof Symbol === "function" && o[Symbol.iterator];
        if (!m) return o;
        var i = m.call(o), r, ar = [], e;
        try {
            while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
        }
        catch (error) { e = { error: error }; }
        finally {
            try {
                if (r && !r.done && (m = i["return"])) m.call(i);
            }
            finally { if (e) throw e.error; }
        }
        return ar;
    };
    var __spread = (this && this.__spread) || function () {
        for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
        return ar;
    };
    function getNtCheckboxGroupNonFunctionValueError() {
        return Error('`compareWith` must be a function.');
    }
    var uniqueId$1 = 0;
    var NtCheckboxGroupComponent = (function (_super) {
        __extends(NtCheckboxGroupComponent, _super);
        function NtCheckboxGroupComponent(_ngZone, _changeDetectorRef, ngControl) {
            var _this = _super.call(this) || this;
            _this._ngZone = _ngZone;
            _this._changeDetectorRef = _changeDetectorRef;
            _this.ngControl = ngControl;
            _this.id = "nt-checkbox-group-" + uniqueId$1++;
            _this._destroy = new rxjs.Subject();
            _this._disabled = false;
            _this._readonly = false;
            _this._required = false;
            _this._compareWith = function (o1, o2) { return o1 === o2; };
            _this.checkedChanges = rxjs.defer(function () {
                if (_this.checkboxes) {
                    return rxjs.merge.apply(void 0, __spread(_this.checkboxes.map(function (item) { return item.change; })));
                }
                return _this._ngZone.onStable
                    .asObservable()
                    .pipe(operators.take(1), operators.switchMap(function () { return _this.checkedChanges; }));
            });
            _this._onChange = function () { };
            _this._onTouched = function () { };
            if (_this.ngControl) {
                _this.ngControl.valueAccessor = _this;
            }
            return _this;
        }
        NtCheckboxGroupComponent_1 = NtCheckboxGroupComponent;
        Object.defineProperty(NtCheckboxGroupComponent.prototype, "value", {
            get: function () { return this._value; },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxGroupComponent.prototype, "disabled", {
            get: function () { return this._disabled; },
            set: function (value) { this._disabled = coercion.coerceBooleanProperty(value); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxGroupComponent.prototype, "required", {
            get: function () { return this._required; },
            set: function (value) { this._required = coercion.coerceBooleanProperty(value); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxGroupComponent.prototype, "readonly", {
            get: function () { return this._readonly; },
            set: function (value) { this._readonly = coercion.coerceBooleanProperty(value); },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(NtCheckboxGroupComponent.prototype, "compareWith", {
            set: function (fn) {
                if (typeof fn !== 'function') {
                    throw getNtCheckboxGroupNonFunctionValueError();
                }
                else {
                    this._compareWith = fn;
                }
            },
            enumerable: true,
            configurable: true
        });
        NtCheckboxGroupComponent.prototype.ngAfterViewInit = function () {
            var _this = this;
            this.checkboxes.changes.pipe(operators.startWith(null), operators.takeUntil(this._destroy)).subscribe(function () {
                _this._resetCheckboxs();
                _this._initializeChecked();
            });
        };
        NtCheckboxGroupComponent.prototype.ngOnDestroy = function () {
            this._destroy.next();
            this._destroy.complete();
        };
        NtCheckboxGroupComponent.prototype.writeValue = function (value) {
            if (this.checkboxes) {
                this._setCheckedByValue(value);
            }
        };
        NtCheckboxGroupComponent.prototype.registerOnChange = function (fn) {
            this._onChange = fn;
        };
        NtCheckboxGroupComponent.prototype.registerOnTouched = function (fn) {
            this._onTouched = fn;
        };
        NtCheckboxGroupComponent.prototype.focus = function () {
            if (!this.disabled) {
                this._onTouched();
            }
        };
        NtCheckboxGroupComponent.prototype._initializeChecked = function () {
            var _this = this;
            Promise.resolve().then(function () {
                _this._setCheckedByValue(_this.ngControl ? _this.ngControl.value : _this.value);
            });
        };
        NtCheckboxGroupComponent.prototype._resetCheckboxs = function () {
            var _this = this;
            var changedOrDestroyed = rxjs.merge(this.checkboxes.changes, this._destroy);
            this.checkedChanges.pipe(operators.takeUntil(changedOrDestroyed)).subscribe(function () {
                _this._setValues();
            });
            rxjs.merge.apply(void 0, __spread(this.checkboxes.map(function (item) { return item.change; }))).pipe(operators.takeUntil(changedOrDestroyed))
                .subscribe(function () {
                _this._changeDetectorRef.markForCheck();
            });
        };
        NtCheckboxGroupComponent.prototype._setCheckedByValue = function (value) {
            var _this = this;
            if (value) {
                this._value = value;
                this.checkboxes.forEach(function (item) { return item.checked = false; });
                value.forEach(function (val) { return _this._checkedValue(val); });
                this._changeDetectorRef.markForCheck();
            }
        };
        NtCheckboxGroupComponent.prototype._checkedValue = function (value) {
            var _this = this;
            var correspondingItem = this.checkboxes.find(function (item) {
                try {
                    return item.value != null && _this._compareWith(item.value, value);
                }
                catch (error) {
                    if (core.isDevMode()) {
                        console.warn(error);
                    }
                    return false;
                }
            });
            if (correspondingItem) {
                correspondingItem.checked = true;
            }
            return correspondingItem;
        };
        NtCheckboxGroupComponent.prototype._setValues = function () {
            var _this = this;
            this._value = [];
            this.checkboxes.forEach(function (item) {
                if (item.checked && !!item.value) {
                    _this._value.push(item.value);
                }
            });
            this._onChange(this._value);
            this._onTouched();
            this._changeDetectorRef.markForCheck();
        };
        var NtCheckboxGroupComponent_1;
        NtCheckboxGroupComponent.ctorParameters = function () { return [
            { type: core.NgZone },
            { type: core.ChangeDetectorRef },
            { type: forms.NgControl, decorators: [{ type: core.Self }, { type: core.Optional }] }
        ]; };
        __decorate$1([
            core.Input()
        ], NtCheckboxGroupComponent.prototype, "disabled", null);
        __decorate$1([
            core.Input()
        ], NtCheckboxGroupComponent.prototype, "required", null);
        __decorate$1([
            core.Input()
        ], NtCheckboxGroupComponent.prototype, "readonly", null);
        __decorate$1([
            core.ContentChildren(NtCheckboxComponent)
        ], NtCheckboxGroupComponent.prototype, "checkboxes", void 0);
        __decorate$1([
            core.Input()
        ], NtCheckboxGroupComponent.prototype, "compareWith", null);
        NtCheckboxGroupComponent = NtCheckboxGroupComponent_1 = __decorate$1([
            core.Component({
                selector: 'nt-checkbox-group',
                template: '<ng-content></ng-content>',
                encapsulation: core.ViewEncapsulation.None,
                changeDetection: core.ChangeDetectionStrategy.OnPush,
                providers: [
                    { provide: forms$1.NtFormFieldControl, useExisting: NtCheckboxGroupComponent_1 }
                ],
                host: {
                    'class': 'nt-checkbox-group'
                }
            }),
            __param$1(2, core.Self()), __param$1(2, core.Optional())
        ], NtCheckboxGroupComponent);
        return NtCheckboxGroupComponent;
    }(forms$1.NtFormFieldControl));

    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 NtCheckboxModule = (function () {
        function NtCheckboxModule() {
        }
        NtCheckboxModule = __decorate$2([
            core.NgModule({
                imports: [common.CommonModule],
                exports: [NtCheckboxComponent, NtCheckboxGroupComponent],
                declarations: [NtCheckboxComponent, NtCheckboxGroupComponent],
            })
        ], NtCheckboxModule);
        return NtCheckboxModule;
    }());

    exports.NtCheckboxChange = NtCheckboxChange;
    exports.NtCheckboxComponent = NtCheckboxComponent;
    exports.NtCheckboxGroupComponent = NtCheckboxGroupComponent;
    exports.NtCheckboxModule = NtCheckboxModule;
    exports.getNtCheckboxGroupNonFunctionValueError = getNtCheckboxGroupNonFunctionValueError;

    Object.defineProperty(exports, '__esModule', { value: true });

})));
//# sourceMappingURL=checkbox.umd.js.map
