import { __decorate, __metadata } from 'tslib';
import { FocusMonitor } from '@angular/cdk/a11y';
import { LEFT_ARROW, RIGHT_ARROW, SPACE, ENTER } from '@angular/cdk/keycodes';
import { ɵɵelement, ɵɵelementContainerStart, ɵɵtext, ɵɵelementContainerEnd, ɵɵnextContext, ɵɵadvance, ɵɵtextInterpolate, ɵɵtemplate, ɵɵproperty, ɵɵdirectiveInject, ChangeDetectorRef, ɵɵdefineComponent, ɵɵstaticViewQuery, ɵɵqueryRefresh, ɵɵloadQuery, ɵɵlistener, ɵɵProvidersFeature, forwardRef, ɵɵelementStart, ɵɵtemplateRefExtractor, ɵɵelementEnd, ɵɵreference, ɵɵclassProp, ɵsetClassMetadata, Component, ChangeDetectionStrategy, ViewEncapsulation, ViewChild, Input, ɵɵdefineNgModule, ɵɵdefineInjector, ɵɵsetNgModuleScope, NgModule } from '@angular/core';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { NzConfigService, NzWaveDirective, NzStringTemplateOutletDirective, InputBoolean, WithConfig, NzWaveModule, NzOutletModule } from 'ng-zorro-antd/core';
import { NgIf, CommonModule } from '@angular/common';
import { NzIconDirective, NzIconModule } from 'ng-zorro-antd/icon';

var _c0 = ["switchElement"];
function NzSwitchComponent_i_2_Template(rf, ctx) { if (rf & 1) {
    ɵɵelement(0, "i", 7);
} }
function NzSwitchComponent_ng_container_4_ng_container_1_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementContainerStart(0);
    ɵɵtext(1);
    ɵɵelementContainerEnd();
} if (rf & 2) {
    var ctx_r382 = ɵɵnextContext(2);
    ɵɵadvance(1);
    ɵɵtextInterpolate(ctx_r382.nzCheckedChildren);
} }
function NzSwitchComponent_ng_container_4_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementContainerStart(0);
    ɵɵtemplate(1, NzSwitchComponent_ng_container_4_ng_container_1_Template, 2, 1, "ng-container", 8);
    ɵɵelementContainerEnd();
} if (rf & 2) {
    var ctx_r379 = ɵɵnextContext();
    ɵɵadvance(1);
    ɵɵproperty("nzStringTemplateOutlet", ctx_r379.nzCheckedChildren);
} }
function NzSwitchComponent_ng_template_5_ng_container_0_Template(rf, ctx) { if (rf & 1) {
    ɵɵelementContainerStart(0);
    ɵɵtext(1);
    ɵɵelementContainerEnd();
} if (rf & 2) {
    var ctx_r383 = ɵɵnextContext(2);
    ɵɵadvance(1);
    ɵɵtextInterpolate(ctx_r383.nzUnCheckedChildren);
} }
function NzSwitchComponent_ng_template_5_Template(rf, ctx) { if (rf & 1) {
    ɵɵtemplate(0, NzSwitchComponent_ng_template_5_ng_container_0_Template, 2, 1, "ng-container", 8);
} if (rf & 2) {
    var ctx_r381 = ɵɵnextContext();
    ɵɵproperty("nzStringTemplateOutlet", ctx_r381.nzUnCheckedChildren);
} }
var NZ_CONFIG_COMPONENT_NAME = 'switch';
var NzSwitchComponent = /** @class */ (function () {
    function NzSwitchComponent(nzConfigService, cdr, focusMonitor) {
        this.nzConfigService = nzConfigService;
        this.cdr = cdr;
        this.focusMonitor = focusMonitor;
        this.isChecked = false;
        this.onChange = function () { };
        this.onTouched = function () { };
        this.nzLoading = false;
        this.nzDisabled = false;
        this.nzControl = false;
        this.nzCheckedChildren = null;
        this.nzUnCheckedChildren = null;
    }
    NzSwitchComponent.prototype.onHostClick = function (e) {
        e.preventDefault();
        if (!this.nzDisabled && !this.nzLoading && !this.nzControl) {
            this.updateValue(!this.isChecked);
        }
    };
    NzSwitchComponent.prototype.updateValue = function (value) {
        if (this.isChecked !== value) {
            this.isChecked = value;
            this.onChange(this.isChecked);
        }
    };
    NzSwitchComponent.prototype.onKeyDown = function (e) {
        if (!this.nzControl && !this.nzDisabled && !this.nzLoading) {
            if (e.keyCode === LEFT_ARROW) {
                this.updateValue(false);
                e.preventDefault();
            }
            else if (e.keyCode === RIGHT_ARROW) {
                this.updateValue(true);
                e.preventDefault();
            }
            else if (e.keyCode === SPACE || e.keyCode === ENTER) {
                this.updateValue(!this.isChecked);
                e.preventDefault();
            }
        }
    };
    NzSwitchComponent.prototype.focus = function () {
        this.focusMonitor.focusVia(this.switchElement.nativeElement, 'keyboard');
    };
    NzSwitchComponent.prototype.blur = function () {
        this.switchElement.nativeElement.blur();
    };
    NzSwitchComponent.prototype.ngAfterViewInit = function () {
        var _this = this;
        this.focusMonitor.monitor(this.switchElement.nativeElement, true).subscribe(function (focusOrigin) {
            if (!focusOrigin) {
                /** https://github.com/angular/angular/issues/17793 **/
                Promise.resolve().then(function () { return _this.onTouched(); });
            }
        });
    };
    NzSwitchComponent.prototype.ngOnDestroy = function () {
        this.focusMonitor.stopMonitoring(this.switchElement.nativeElement);
    };
    NzSwitchComponent.prototype.writeValue = function (value) {
        this.isChecked = value;
        this.cdr.markForCheck();
    };
    NzSwitchComponent.prototype.registerOnChange = function (fn) {
        this.onChange = fn;
    };
    NzSwitchComponent.prototype.registerOnTouched = function (fn) {
        this.onTouched = fn;
    };
    NzSwitchComponent.prototype.setDisabledState = function (disabled) {
        this.nzDisabled = disabled;
        this.cdr.markForCheck();
    };
    /** @nocollapse */ NzSwitchComponent.ɵfac = function NzSwitchComponent_Factory(t) { return new (t || NzSwitchComponent)(ɵɵdirectiveInject(NzConfigService), ɵɵdirectiveInject(ChangeDetectorRef), ɵɵdirectiveInject(FocusMonitor)); };
    /** @nocollapse */ NzSwitchComponent.ɵcmp = ɵɵdefineComponent({ type: NzSwitchComponent, selectors: [["nz-switch"]], viewQuery: function NzSwitchComponent_Query(rf, ctx) { if (rf & 1) {
            ɵɵstaticViewQuery(_c0, true);
        } if (rf & 2) {
            var _t;
            ɵɵqueryRefresh(_t = ɵɵloadQuery()) && (ctx.switchElement = _t.first);
        } }, hostBindings: function NzSwitchComponent_HostBindings(rf, ctx) { if (rf & 1) {
            ɵɵlistener("click", function NzSwitchComponent_click_HostBindingHandler($event) { return ctx.onHostClick($event); });
        } }, inputs: { nzLoading: "nzLoading", nzDisabled: "nzDisabled", nzControl: "nzControl", nzCheckedChildren: "nzCheckedChildren", nzUnCheckedChildren: "nzUnCheckedChildren", nzSize: "nzSize" }, exportAs: ["nzSwitch"], features: [ɵɵProvidersFeature([
                {
                    provide: NG_VALUE_ACCESSOR,
                    useExisting: forwardRef((function () { return NzSwitchComponent; })),
                    multi: true
                }
            ])], decls: 8, vars: 13, consts: [["nz-wave", "", "type", "button", 1, "ant-switch", 3, "disabled", "nzWaveExtraNode", "keydown"], ["switchElement", ""], ["nz-icon", "", "nzType", "loading", "class", "ant-switch-loading-icon", 4, "ngIf"], [1, "ant-switch-inner"], [4, "ngIf", "ngIfElse"], ["uncheckTemplate", ""], [1, "ant-click-animating-node"], ["nz-icon", "", "nzType", "loading", 1, "ant-switch-loading-icon"], [4, "nzStringTemplateOutlet"]], template: function NzSwitchComponent_Template(rf, ctx) { if (rf & 1) {
            ɵɵelementStart(0, "button", 0, 1);
            ɵɵlistener("keydown", function NzSwitchComponent_Template_button_keydown_0_listener($event) { return ctx.onKeyDown($event); });
            ɵɵtemplate(2, NzSwitchComponent_i_2_Template, 1, 0, "i", 2);
            ɵɵelementStart(3, "span", 3);
            ɵɵtemplate(4, NzSwitchComponent_ng_container_4_Template, 2, 1, "ng-container", 4);
            ɵɵtemplate(5, NzSwitchComponent_ng_template_5_Template, 1, 1, "ng-template", null, 5, ɵɵtemplateRefExtractor);
            ɵɵelementEnd();
            ɵɵelement(7, "div", 6);
            ɵɵelementEnd();
        } if (rf & 2) {
            var _r380 = ɵɵreference(6);
            ɵɵclassProp("ant-switch-checked", ctx.isChecked)("ant-switch-loading", ctx.nzLoading)("ant-switch-disabled", ctx.nzDisabled)("ant-switch-small", ctx.nzSize === "small");
            ɵɵproperty("disabled", ctx.nzDisabled)("nzWaveExtraNode", true);
            ɵɵadvance(2);
            ɵɵproperty("ngIf", ctx.nzLoading);
            ɵɵadvance(2);
            ɵɵproperty("ngIf", ctx.isChecked)("ngIfElse", _r380);
        } }, directives: [NzWaveDirective, NgIf, NzIconDirective, NzStringTemplateOutletDirective], encapsulation: 2, changeDetection: 0 });
    __decorate([
        InputBoolean(),
        __metadata("design:type", Object)
    ], NzSwitchComponent.prototype, "nzLoading", void 0);
    __decorate([
        InputBoolean(),
        __metadata("design:type", Object)
    ], NzSwitchComponent.prototype, "nzDisabled", void 0);
    __decorate([
        InputBoolean(),
        __metadata("design:type", Object)
    ], NzSwitchComponent.prototype, "nzControl", void 0);
    __decorate([
        WithConfig(NZ_CONFIG_COMPONENT_NAME, 'default'),
        __metadata("design:type", String)
    ], NzSwitchComponent.prototype, "nzSize", void 0);
    return NzSwitchComponent;
}());
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzSwitchComponent, [{
        type: Component,
        args: [{
                selector: 'nz-switch',
                exportAs: 'nzSwitch',
                preserveWhitespaces: false,
                changeDetection: ChangeDetectionStrategy.OnPush,
                encapsulation: ViewEncapsulation.None,
                providers: [
                    {
                        provide: NG_VALUE_ACCESSOR,
                        useExisting: forwardRef((function () { return NzSwitchComponent; })),
                        multi: true
                    }
                ],
                template: "\n    <button\n      nz-wave\n      type=\"button\"\n      class=\"ant-switch\"\n      #switchElement\n      [disabled]=\"nzDisabled\"\n      [class.ant-switch-checked]=\"isChecked\"\n      [class.ant-switch-loading]=\"nzLoading\"\n      [class.ant-switch-disabled]=\"nzDisabled\"\n      [class.ant-switch-small]=\"nzSize === 'small'\"\n      [nzWaveExtraNode]=\"true\"\n      (keydown)=\"onKeyDown($event)\"\n    >\n      <i *ngIf=\"nzLoading\" nz-icon nzType=\"loading\" class=\"ant-switch-loading-icon\"></i>\n      <span class=\"ant-switch-inner\">\n        <ng-container *ngIf=\"isChecked; else uncheckTemplate\">\n          <ng-container *nzStringTemplateOutlet=\"nzCheckedChildren\">{{ nzCheckedChildren }}</ng-container>\n        </ng-container>\n        <ng-template #uncheckTemplate>\n          <ng-container *nzStringTemplateOutlet=\"nzUnCheckedChildren\">{{ nzUnCheckedChildren }}</ng-container>\n        </ng-template>\n      </span>\n      <div class=\"ant-click-animating-node\"></div>\n    </button>\n  ",
                host: {
                    '(click)': 'onHostClick($event)'
                }
            }]
    }], function () { return [{ type: NzConfigService }, { type: ChangeDetectorRef }, { type: FocusMonitor }]; }, { switchElement: [{
            type: ViewChild,
            args: ['switchElement', { static: true }]
        }], nzLoading: [{
            type: Input
        }], nzDisabled: [{
            type: Input
        }], nzControl: [{
            type: Input
        }], nzCheckedChildren: [{
            type: Input
        }], nzUnCheckedChildren: [{
            type: Input
        }], nzSize: [{
            type: Input
        }] }); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */
var NzSwitchModule = /** @class */ (function () {
    function NzSwitchModule() {
    }
    /** @nocollapse */ NzSwitchModule.ɵmod = ɵɵdefineNgModule({ type: NzSwitchModule });
    /** @nocollapse */ NzSwitchModule.ɵinj = ɵɵdefineInjector({ factory: function NzSwitchModule_Factory(t) { return new (t || NzSwitchModule)(); }, imports: [[CommonModule, NzWaveModule, NzIconModule, NzOutletModule]] });
    return NzSwitchModule;
}());
(function () { (typeof ngJitMode === "undefined" || ngJitMode) && ɵɵsetNgModuleScope(NzSwitchModule, { declarations: [NzSwitchComponent], imports: [CommonModule, NzWaveModule, NzIconModule, NzOutletModule], exports: [NzSwitchComponent] }); })();
/*@__PURE__*/ (function () { ɵsetClassMetadata(NzSwitchModule, [{
        type: NgModule,
        args: [{
                exports: [NzSwitchComponent],
                declarations: [NzSwitchComponent],
                imports: [CommonModule, NzWaveModule, NzIconModule, NzOutletModule]
            }]
    }], null, null); })();

/**
 * @license
 * Copyright Alibaba.com All Rights Reserved.
 *
 * Use of this source code is governed by an MIT-style license that can be
 * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
 */

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

export { NzSwitchComponent, NzSwitchModule };
//# sourceMappingURL=ng-zorro-antd-switch.js.map
