import { EventEmitter, Component, ChangeDetectorRef, Input, Output, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCheckboxModule } from '@angular/material/checkbox';
import { MatFormFieldModule } from '@angular/material/form-field';
import { FormControl, NgForm, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
import { Subscription } from 'rxjs';
import { v4 } from 'uuid';

var _name, _form, _subscription, _model, _onChange;
class SdCheckbox {
    constructor(ref) {
        this.ref = ref;
        this.id = `I${v4()}`;
        _name.set(this, v4());
        _form.set(this, void 0);
        this.formControl = new FormControl();
        _subscription.set(this, new Subscription());
        this.color = 'primary';
        _model.set(this, void 0);
        this.modelChange = new EventEmitter();
        this.sdChange = new EventEmitter();
        _onChange.set(this, (value) => {
            this.modelChange.emit(value);
            this.sdChange.emit(value);
        });
    }
    set name(val) {
        if (val) {
            __classPrivateFieldSet(this, _name, val);
        }
    }
    set form(val) {
        if (val) {
            if (val instanceof NgForm) {
                __classPrivateFieldSet(this, _form, val.form);
            }
            else {
                __classPrivateFieldSet(this, _form, val);
            }
        }
    }
    set disabled(val) {
        val = (val === '') || val;
        if (val) {
            this.formControl.disable();
        }
        else {
            this.formControl.enable();
        }
    }
    set model(value) {
        if (__classPrivateFieldGet(this, _model) !== value) {
            __classPrivateFieldSet(this, _model, value);
            this.formControl.setValue(value, {
                emitEvent: false
            });
        }
    }
    ngOnInit() {
    }
    ngAfterViewInit() {
        var _a;
        __classPrivateFieldGet(this, _subscription).add(this.formControl.valueChanges.subscribe(__classPrivateFieldGet(this, _onChange)));
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.addControl(__classPrivateFieldGet(this, _name), this.formControl);
        this.ref.detectChanges();
    }
    ngOnDestroy() {
        var _a;
        (_a = __classPrivateFieldGet(this, _form)) === null || _a === void 0 ? void 0 : _a.removeControl(__classPrivateFieldGet(this, _name));
        __classPrivateFieldGet(this, _subscription).unsubscribe();
    }
}
_name = new WeakMap(), _form = new WeakMap(), _subscription = new WeakMap(), _model = new WeakMap(), _onChange = new WeakMap();
SdCheckbox.decorators = [
    { type: Component, args: [{
                selector: 'sd-checkbox',
                template: "<mat-checkbox [formControl]=\"formControl\" [color]=\"color\">{{label}}</mat-checkbox>"
            },] }
];
SdCheckbox.ctorParameters = () => [
    { type: ChangeDetectorRef }
];
SdCheckbox.propDecorators = {
    name: [{ type: Input }],
    form: [{ type: Input }],
    label: [{ type: Input }],
    color: [{ type: Input }],
    disabled: [{ type: Input }],
    model: [{ type: Input }],
    modelChange: [{ type: Output }],
    sdChange: [{ type: Output }]
};

class SdCheckboxModule {
}
SdCheckboxModule.decorators = [
    { type: NgModule, args: [{
                imports: [
                    CommonModule,
                    FormsModule,
                    ReactiveFormsModule,
                    MatFormFieldModule,
                    MatCheckboxModule
                ],
                declarations: [
                    SdCheckbox
                ],
                exports: [
                    SdCheckbox
                ]
            },] }
];

/**
 * Generated bundle index. Do not edit.
 */

export { SdCheckboxModule, SdCheckbox as ɵa };
//# sourceMappingURL=sd-angular-core-checkbox.js.map
