import { __esDecorate, __runInitializers } from 'tslib';
import { FocusMonitor } from '@angular/cdk/a11y';
import { Directionality } from '@angular/cdk/bidi';
import { LEFT_ARROW, RIGHT_ARROW, SPACE, ENTER } from '@angular/cdk/keycodes';
import * as i0 from '@angular/core';
import { inject, ElementRef, NgZone, ChangeDetectorRef, DestroyRef, signal, computed, forwardRef, booleanAttribute, Input, ViewChild, ViewEncapsulation, Component, NgModule } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { NG_VALUE_ACCESSOR } from '@angular/forms';
import { WithConfig, NzConfigService } from 'ng-zorro-antd/core/config';
import { NZ_FORM_SIZE } from 'ng-zorro-antd/core/form';
import * as i3 from 'ng-zorro-antd/core/outlet';
import { NzOutletModule } from 'ng-zorro-antd/core/outlet';
import { fromEventOutsideAngular } from 'ng-zorro-antd/core/util';
import * as i1 from 'ng-zorro-antd/core/wave';
import { NzWaveModule } from 'ng-zorro-antd/core/wave';
import * as i2 from 'ng-zorro-antd/icon';
import { NzIconModule } from 'ng-zorro-antd/icon';

const NZ_CONFIG_MODULE_NAME = 'switch';
let NzSwitchComponent = (() => {
    let _nzSize_decorators;
    let _nzSize_initializers = [];
    let _nzSize_extraInitializers = [];
    return class NzSwitchComponent {
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            _nzSize_decorators = [WithConfig()];
            __esDecorate(null, null, _nzSize_decorators, { kind: "field", name: "nzSize", static: false, private: false, access: { has: obj => "nzSize" in obj, get: obj => obj.nzSize, set: (obj, value) => { obj.nzSize = value; } }, metadata: _metadata }, _nzSize_initializers, _nzSize_extraInitializers);
            if (_metadata) Object.defineProperty(this, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
        }
        _nzModuleName = NZ_CONFIG_MODULE_NAME;
        nzConfigService = inject(NzConfigService);
        el = inject((ElementRef)).nativeElement;
        ngZone = inject(NgZone);
        cdr = inject(ChangeDetectorRef);
        focusMonitor = inject(FocusMonitor);
        destroyRef = inject(DestroyRef);
        isChecked = false;
        onChange = () => { };
        onTouched = () => { };
        switchElement;
        nzLoading = false;
        nzDisabled = false;
        nzControl = false;
        nzCheckedChildren = null;
        nzUnCheckedChildren = null;
        nzSize = __runInitializers(this, _nzSize_initializers, 'default');
        nzId = (__runInitializers(this, _nzSize_extraInitializers), null);
        dir = inject(Directionality).valueSignal;
        isNzDisableFirstChange = true;
        size = signal(this.nzSize, /* @ts-ignore */
        ...(ngDevMode ? [{ debugName: "size" }] : /* istanbul ignore next */ []));
        formSize = inject(NZ_FORM_SIZE, { optional: true });
        finalSize = computed(() => this.formSize?.() || this.size(), /* @ts-ignore */
        ...(ngDevMode ? [{ debugName: "finalSize" }] : /* istanbul ignore next */ []));
        updateValue(value) {
            if (this.isChecked !== value) {
                this.isChecked = value;
                this.onChange(this.isChecked);
            }
        }
        focus() {
            this.focusMonitor.focusVia(this.switchElement.nativeElement, 'keyboard');
        }
        blur() {
            this.switchElement.nativeElement.blur();
        }
        constructor() {
            this.destroyRef.onDestroy(() => {
                this.focusMonitor.stopMonitoring(this.switchElement.nativeElement);
            });
        }
        ngOnChanges(changes) {
            const { nzSize } = changes;
            if (nzSize) {
                this.size.set(nzSize.currentValue);
            }
        }
        ngOnInit() {
            fromEventOutsideAngular(this.el, 'click')
                .pipe(takeUntilDestroyed(this.destroyRef))
                .subscribe(event => {
                event.preventDefault();
                if (this.nzControl || this.nzDisabled || this.nzLoading) {
                    return;
                }
                this.ngZone.run(() => {
                    this.updateValue(!this.isChecked);
                    this.cdr.markForCheck();
                });
            });
            fromEventOutsideAngular(this.switchElement.nativeElement, 'keydown')
                .pipe(takeUntilDestroyed(this.destroyRef))
                .subscribe(event => {
                if (this.nzControl || this.nzDisabled || this.nzLoading) {
                    return;
                }
                const { keyCode } = event;
                if (keyCode !== LEFT_ARROW && keyCode !== RIGHT_ARROW && keyCode !== SPACE && keyCode !== ENTER) {
                    return;
                }
                event.preventDefault();
                this.ngZone.run(() => {
                    if (keyCode === LEFT_ARROW) {
                        this.updateValue(false);
                    }
                    else if (keyCode === RIGHT_ARROW) {
                        this.updateValue(true);
                    }
                    else if (keyCode === SPACE || keyCode === ENTER) {
                        this.updateValue(!this.isChecked);
                    }
                    this.cdr.markForCheck();
                });
            });
        }
        ngAfterViewInit() {
            this.focusMonitor
                .monitor(this.switchElement.nativeElement, true)
                .pipe(takeUntilDestroyed(this.destroyRef))
                .subscribe(focusOrigin => {
                if (!focusOrigin) {
                    /** https://github.com/angular/angular/issues/17793 **/
                    Promise.resolve().then(() => this.onTouched());
                }
            });
        }
        writeValue(value) {
            this.isChecked = value;
            this.cdr.markForCheck();
        }
        registerOnChange(fn) {
            this.onChange = fn;
        }
        registerOnTouched(fn) {
            this.onTouched = fn;
        }
        setDisabledState(disabled) {
            this.nzDisabled = (this.isNzDisableFirstChange && this.nzDisabled) || disabled;
            this.isNzDisableFirstChange = false;
            this.cdr.markForCheck();
        }
        static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchComponent, deps: [], target: i0.ɵɵFactoryTarget.Component });
        static ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "22.0.6", type: NzSwitchComponent, isStandalone: true, selector: "nz-switch", inputs: { nzLoading: ["nzLoading", "nzLoading", booleanAttribute], nzDisabled: ["nzDisabled", "nzDisabled", booleanAttribute], nzControl: ["nzControl", "nzControl", booleanAttribute], nzCheckedChildren: "nzCheckedChildren", nzUnCheckedChildren: "nzUnCheckedChildren", nzSize: "nzSize", nzId: "nzId" }, providers: [
                {
                    provide: NG_VALUE_ACCESSOR,
                    useExisting: forwardRef(() => NzSwitchComponent),
                    multi: true
                }
            ], viewQueries: [{ propertyName: "switchElement", first: true, predicate: ["switchElement"], descendants: true, static: true }], exportAs: ["nzSwitch"], usesOnChanges: true, ngImport: i0, template: `
    <button
      nz-wave
      type="button"
      class="ant-switch"
      #switchElement
      [attr.id]="nzId"
      [disabled]="nzDisabled"
      [class.ant-switch-checked]="isChecked"
      [class.ant-switch-loading]="nzLoading"
      [class.ant-switch-disabled]="nzDisabled"
      [class.ant-switch-small]="finalSize() === 'small'"
      [class.ant-switch-rtl]="dir() === 'rtl'"
      [nzWaveExtraNode]="true"
    >
      <span class="ant-switch-handle">
        @if (nzLoading) {
          <nz-icon nzType="loading" class="ant-switch-loading-icon" />
        }
      </span>
      <span class="ant-switch-inner">
        @if (isChecked) {
          <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container>
        } @else {
          <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container>
        }
      </span>
      <div class="ant-click-animating-node"></div>
    </button>
  `, isInline: true, dependencies: [{ kind: "ngmodule", type: NzWaveModule }, { kind: "directive", type: i1.NzWaveDirective, selector: "[nz-wave],button[nz-button]:not([nzType=\"link\"]):not([nzType=\"text\"])", inputs: ["nzWaveExtraNode"], exportAs: ["nzWave"] }, { kind: "ngmodule", type: NzIconModule }, { kind: "directive", type: i2.NzIconDirective, selector: "nz-icon,[nz-icon]", inputs: ["nzType", "nzTheme", "nzTwotoneColor", "nzSpin", "nzRotate", "nzIconfont"], exportAs: ["nzIcon"] }, { kind: "ngmodule", type: NzOutletModule }, { kind: "directive", type: i3.NzStringTemplateOutletDirective, selector: "[nzStringTemplateOutlet]", inputs: ["nzStringTemplateOutletContext", "nzStringTemplateOutlet"], exportAs: ["nzStringTemplateOutlet"] }], encapsulation: i0.ViewEncapsulation.None });
    };
})();
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchComponent, decorators: [{
            type: Component,
            args: [{
                    selector: 'nz-switch',
                    exportAs: 'nzSwitch',
                    encapsulation: ViewEncapsulation.None,
                    providers: [
                        {
                            provide: NG_VALUE_ACCESSOR,
                            useExisting: forwardRef(() => NzSwitchComponent),
                            multi: true
                        }
                    ],
                    template: `
    <button
      nz-wave
      type="button"
      class="ant-switch"
      #switchElement
      [attr.id]="nzId"
      [disabled]="nzDisabled"
      [class.ant-switch-checked]="isChecked"
      [class.ant-switch-loading]="nzLoading"
      [class.ant-switch-disabled]="nzDisabled"
      [class.ant-switch-small]="finalSize() === 'small'"
      [class.ant-switch-rtl]="dir() === 'rtl'"
      [nzWaveExtraNode]="true"
    >
      <span class="ant-switch-handle">
        @if (nzLoading) {
          <nz-icon nzType="loading" class="ant-switch-loading-icon" />
        }
      </span>
      <span class="ant-switch-inner">
        @if (isChecked) {
          <ng-container *nzStringTemplateOutlet="nzCheckedChildren">{{ nzCheckedChildren }}</ng-container>
        } @else {
          <ng-container *nzStringTemplateOutlet="nzUnCheckedChildren">{{ nzUnCheckedChildren }}</ng-container>
        }
      </span>
      <div class="ant-click-animating-node"></div>
    </button>
  `,
                    imports: [NzWaveModule, NzIconModule, NzOutletModule]
                }]
        }], ctorParameters: () => [], propDecorators: { switchElement: [{
                type: ViewChild,
                args: ['switchElement', { static: true }]
            }], nzLoading: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], nzDisabled: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], nzControl: [{
                type: Input,
                args: [{ transform: booleanAttribute }]
            }], nzCheckedChildren: [{
                type: Input
            }], nzUnCheckedChildren: [{
                type: Input
            }], nzSize: [{
                type: Input
            }], nzId: [{
                type: Input
            }] } });

/**
 * 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
 */
class NzSwitchModule {
    static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
    static ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent], exports: [NzSwitchComponent] });
    static ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchModule, imports: [NzSwitchComponent] });
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.6", ngImport: i0, type: NzSwitchModule, decorators: [{
            type: NgModule,
            args: [{
                    imports: [NzSwitchComponent],
                    exports: [NzSwitchComponent]
                }]
        }] });

/**
 * 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.mjs.map
