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;
};
import { Element, Component, Host, h, Prop, Event, Watch } from '@stencil/core';
import { VegaChange } from '../../helpers/event-manager/event-id/vega-event-id';
import { createEventEmitSlimmer, } from '../../helpers/event-manager/slimmers/event-emit-slimmer';
import { createEnterKeyHandlerToTriggerClick } from '../../utils/accessibility';
import { sanitizeVegaComponent } from '../../utils/component';
import { InjectVegaGlobalSlimmer, InjectVegaSlimmer, methodPlaceholder, } from 'vega-slimmer/core';
import { VegaComponentUsageRuntimeMetricsSlimmer } from '../../helpers/slimmers/component-usage-runtime-metrics';
import { FormFieldControllerSlimmer } from '../../helpers/slimmers/form-field-controller-slimmer';
import { ToggleSwitchRequiredRule } from '../../helpers/validator/rules/toggle-switch-required-rule';
import { FORM_ELEMENT_VALUE_CHANGE } from '../../helpers/change-manager/subject/static-subject/static-subject-title';
import ChangeManager from '../../helpers/change-manager/change-manager';
/**
 * @vegaVersion 1.3.0
 */
export class VegaToggleSwitch {
  constructor() {
    this.globalSlimmers = {};
    this.vegaComponentUsageRuntimeMetricsSlimmer = new VegaComponentUsageRuntimeMetricsSlimmer();
    this.formFieldController = new FormFieldControllerSlimmer({
      propertyRules: [
        {
          propertyName: 'required',
          ruleFactory: (isRequired) => isRequired ? new ToggleSwitchRequiredRule() : null,
        },
      ],
      validationRulesName: 'validationRules',
      defaultValue: false,
      attributeGetter: (propertyName) => this[propertyName],
      inputContainerGetter: () => this.toggleSwitchContainer,
      touchableAreaGetter: () => [this.toggleSwitchContainer],
    });
    this.changeEventEmitter = createEventEmitSlimmer(VegaToggleSwitch, VegaChange);
    /**
     * Specifies whether the toggle switch is in the checked state.
     *
     * @vegaVersion 1.3.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.checked = false;
    /**
     * The `value` property returns `true` when the toggle switch is checked, and `false` when it is unchecked.
     * This property is included for form control compatibility and can be used interchangeably with the checked property.
     *
     * @vegaVersion 2.25.0
     */
    this.value = false;
    /**
     * Specifies the size of the toggle switch.
     *
     * @vegaVersion 1.3.0
     */
    this.size = 'default';
    /**
     * Specifies whether the toggle switch must be checked
     *    to pass form validation.
     *
     * @vegaVersion 2.25.0
     */
    this.required = false;
    /**
     * Specifies whether the toggle switch is disabled.
     *
     * @vegaVersion 1.3.0
     */
    this.disabled = false;
    /**
     * Defines custom validation rules for the toggle switch.
     *
     * @vegaVersion 2.25.0
     */
    this.validationRules = [];
    /**
     * Determines whether automatic validation should be
     *    implemented for the toggle switch.
     *
     * @vegaVersion 2.25.0
     */
    this.autoValidation = true;
    /**
     * Specifies the validation status of the toggle switch.
     *
     * @vegaVersion 2.25.0
     */
    /* eslint-disable-next-line @stencil/strict-mutable */
    this.isValid = null;
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-toggle-switch-click-on-label-e2e}
     */
    /* istanbul ignore next */
    this.toggleSwitch = () => {
      if (!this.disabled) {
        this.checked = !this.checked;
      }
    };
    /**
     * The below method is e2e-test covered in
     * @see{module:vega-toggle-switch-change}
     * @see{module:vega-toggle-switch-click-with-disabled}
     */
    /* istanbul ignore next */
    this.updateValue = (e) => {
      const currentTarget = e.currentTarget;
      this.checked = currentTarget.checked;
      e.stopPropagation();
    };
  }
  watchCheckedChange() {
    this.value = this.checked;
    this.changeEventEmitter.emit(this.checked);
    ChangeManager.notify(FORM_ELEMENT_VALUE_CHANGE, {
      host: this.host,
      detail: this.value,
    });
  }
  watchValue(newValue) {
    this.checked = newValue === true;
  }
  watchValidationRules() {
    methodPlaceholder();
  }
  componentWillLoad() {
    if (!this.checked && this.value === true) {
      this.checked = true;
    }
    else {
      this.value = this.checked;
    }
  }
  render() {
    let divContainerCss = this.checked
      ? 'vega-container vega-container-checked'
      : 'vega-container';
    divContainerCss += this.disabled ? ' vega-container-disabled' : '';
    divContainerCss += this.size === 'small' ? ' vega-container-small-size' : '';
    return sanitizeVegaComponent(h(Host, null,
      h("vega-flex", { gap: "size-8", ref: (node) => (this.toggleSwitchContainer = node) },
        h("div", { class: `${divContainerCss} vega-flex-shrink-0 v-min-w-min` },
          h("input", { type: "checkbox", name: "switch", disabled: this.disabled, checked: this.checked, onChange: this.updateValue, "aria-label": this.label || 'No label defined', role: 'button', tabIndex: 0, onKeyDown: createEnterKeyHandlerToTriggerClick() }),
          h("span", { class: this.disabled ? 'v-bg-tertiary' : 'v-bg-primary' })),
        this.renderLabelDom())), this.host);
  }
  renderLabelDom() {
    return this.label ? (h("div", { class: "vega-flex-basis-0 vega-flex-grow" },
      h("vega-field-label", { onClick: this.toggleSwitch, disabled: this.disabled, label: this.label, isFieldRequired: this.required }))) : null;
  }
  static get is() { return "vega-toggle-switch"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() { return {
    "$": ["vega-toggle-switch.scss"]
  }; }
  static get styleUrls() { return {
    "$": ["vega-toggle-switch.css"]
  }; }
  static get properties() { return {
    "checked": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies whether the toggle switch is in the checked state."
      },
      "attribute": "checked",
      "reflect": true,
      "defaultValue": "false"
    },
    "value": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.25.0"
          }],
        "text": "The `value` property returns `true` when the toggle switch is checked, and `false` when it is unchecked.\nThis property is included for form control compatibility and can be used interchangeably with the checked property."
      },
      "attribute": "value",
      "reflect": true,
      "defaultValue": "false"
    },
    "size": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "'default' | 'small'",
        "resolved": "\"default\" | \"small\"",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies the size of the toggle switch."
      },
      "attribute": "size",
      "reflect": false,
      "defaultValue": "'default'"
    },
    "label": {
      "type": "string",
      "mutable": false,
      "complexType": {
        "original": "string",
        "resolved": "string",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies the label or title for the toggle switch."
      },
      "attribute": "label",
      "reflect": false
    },
    "required": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.25.0"
          }],
        "text": "Specifies whether the toggle switch must be checked\n   to pass form validation."
      },
      "attribute": "required",
      "reflect": true,
      "defaultValue": "false"
    },
    "disabled": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "Specifies whether the toggle switch is disabled."
      },
      "attribute": "disabled",
      "reflect": true,
      "defaultValue": "false"
    },
    "validationRules": {
      "type": "unknown",
      "mutable": false,
      "complexType": {
        "original": "FormFieldValidationRule<unknown>[]",
        "resolved": "FormFieldValidationRule<unknown>[]",
        "references": {
          "FormFieldValidationRule": {
            "location": "import",
            "path": "../../helpers/validator/rules/form-field-validation-rule"
          }
        }
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.25.0"
          }],
        "text": "Defines custom validation rules for the toggle switch."
      },
      "defaultValue": "[]"
    },
    "autoValidation": {
      "type": "boolean",
      "mutable": false,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.25.0"
          }],
        "text": "Determines whether automatic validation should be\n   implemented for the toggle switch."
      },
      "attribute": "auto-validation",
      "reflect": true,
      "defaultValue": "true"
    },
    "isValid": {
      "type": "boolean",
      "mutable": true,
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      },
      "required": false,
      "optional": false,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "2.25.0"
          }],
        "text": "Specifies the validation status of the toggle switch."
      },
      "attribute": "is-valid",
      "reflect": true,
      "defaultValue": "null"
    }
  }; }
  static get events() { return [{
      "method": "vegaChange",
      "name": "vegaChange",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "vegaVersion",
            "text": "1.3.0"
          }],
        "text": "An event emitter notifying changes in the toggle switch."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }, {
      "method": "change",
      "name": "change",
      "bubbles": true,
      "cancelable": true,
      "composed": true,
      "docs": {
        "tags": [{
            "name": "eventSemantics",
            "text": "namespace:native"
          }, {
            "name": "vegaVersion",
            "text": "2.0.0"
          }],
        "text": "An event emitter notifying changes in the toggle switch."
      },
      "complexType": {
        "original": "boolean",
        "resolved": "boolean",
        "references": {}
      }
    }]; }
  static get elementRef() { return "host"; }
  static get watchers() { return [{
      "propName": "checked",
      "methodName": "watchCheckedChange"
    }, {
      "propName": "value",
      "methodName": "watchValue"
    }, {
      "propName": "validationRules",
      "methodName": "watchValidationRules"
    }]; }
}
__decorate([
  InjectVegaGlobalSlimmer()
], VegaToggleSwitch.prototype, "globalSlimmers", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaToggleSwitch.prototype, "vegaComponentUsageRuntimeMetricsSlimmer", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaToggleSwitch.prototype, "formFieldController", void 0);
__decorate([
  InjectVegaSlimmer()
], VegaToggleSwitch.prototype, "changeEventEmitter", void 0);
