import { html, nothing } from 'lit';
import { query, state, property } from 'lit/decorators.js';
import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import { live } from 'lit/directives/live.js';
import { boolean } from '../../internal/boolean.mjs';
import { defaultValue } from '../../internal/default-value.mjs';
import { FlowRdfFormControl } from '../../internal/flow-ui-rdf-element.mjs';
import { FormControlController } from '../../internal/form.mjs';
import { HasSlotController } from '../../internal/slot.mjs';
import { watch } from '../../internal/watch.mjs';
import styles from './switch.styles.mjs';

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result)
    __defProp(target, key, result);
  return result;
};
class FlowUiSwitch extends FlowRdfFormControl {
  constructor() {
    super(...arguments);
    this.formControlController = new FormControlController(this, {
      value: (control) => control.checked ? control.value || "on" : void 0,
      defaultValue: (control) => control.defaultChecked,
      setValue: (control, checked) => control.checked = checked
    });
    this.hasSlotController = new HasSlotController(this, "help-text", "[default]");
    this.hasFocus = false;
    this.label = "";
    this.name = "";
    this.size = "medium";
    this.disabled = false;
    this.checked = false;
    this.defaultChecked = false;
    this.form = "";
    this.required = false;
    this.helpText = "";
    this.divider = false;
  }
  connectedCallback() {
    super.connectedCallback();
    this.checked = boolean(this.checked, true);
    this.disabled = boolean(this.disabled, true);
    this.required = boolean(this.required, true);
  }
  /** Gets the validity state object */
  get validity() {
    return this.input.validity;
  }
  /** Gets the validation message */
  get validationMessage() {
    return this.input.validationMessage;
  }
  firstUpdated() {
    this.formControlController.updateValidity();
  }
  handleBlur() {
    this.hasFocus = false;
    this.emit("flow-ui-blur");
  }
  handleInput() {
    this.emit("flow-ui-input");
  }
  handleInvalid(event) {
    this.formControlController.setValidity(false);
    this.formControlController.emitInvalidEvent(event);
  }
  handleClick() {
    this.checked = !this.checked;
    this.emit("flow-ui-change");
  }
  handleFocus() {
    this.hasFocus = true;
    this.emit("flow-ui-focus");
  }
  handleKeyDown(event) {
    if (event.key === "ArrowLeft") {
      event.preventDefault();
      this.checked = false;
      this.emit("flow-ui-change");
      this.emit("flow-ui-input");
    }
    if (event.key === "ArrowRight") {
      event.preventDefault();
      this.checked = true;
      this.emit("flow-ui-change");
      this.emit("flow-ui-input");
    }
  }
  handleCheckedChange() {
    this.input.checked = this.checked;
    this.formControlController.updateValidity();
  }
  handleDisabledChange() {
    this.formControlController.setValidity(true);
  }
  /** Simulates a click on the switch. */
  click() {
    this.input.click();
  }
  /** Sets focus on the switch. */
  focus(options) {
    this.input.focus(options);
  }
  /** Removes focus from the switch. */
  blur() {
    this.input.blur();
  }
  /** Checks for validity but does not show a validation message. Returns `true` when valid and `false` when invalid. */
  checkValidity() {
    return this.input.checkValidity();
  }
  /** Gets the associated form, if one exists. */
  getForm() {
    return this.formControlController.getForm();
  }
  /** Checks for validity and shows the browser's validation message if the control is invalid. */
  reportValidity() {
    return this.input.reportValidity();
  }
  /** Sets a custom validation message. Pass an empty string to restore validity. */
  setCustomValidity(message) {
    this.input.setCustomValidity(message);
    this.formControlController.updateValidity();
  }
  render() {
    const hasLabel = this.hasSlotController.test("[default]");
    const hasHelpTextSlot = this.hasSlotController.test("help-text");
    const hasHelpText = this.helpText ? true : !!hasHelpTextSlot;
    return html`
      <div
        part="form-control"
        class=${classMap({
      "form-control": true,
      "form-control--small": this.size === "small",
      "form-control--medium": this.size === "medium",
      "form-control--large": this.size === "large",
      "form-control--inline": true,
      "form-control--disabled": this.disabled,
      "form-control--focused": this.hasFocus,
      "form-control--has-label": hasLabel,
      "form-control--has-help-text": hasHelpText,
      "form-control--has-divider": this.divider,
      "switch--checked": this.checked,
      "switch--disabled": this.disabled,
      "switch--focused": this.hasFocus
    })}
      >
        <label for="input" part="form-control-label" class="form-control__label">
          <slot part="label" class="switch__label"></slot>
        </label>

        <div
          part="form-control-help-text"
          class="form-control__help-text"
          aria-hidden=${hasHelpText ? "false" : "true"}
        >
          <slot name="help-text">${this.helpText}</slot>
        </div>

        <label part="form-control-input" class="form-control__input">
          <input
            id="input"
            class="switch__input"
            type="checkbox"
            title=${this.label}
            name=${this.name}
            value=${ifDefined(this.value)}
            .checked=${live(this.checked)}
            .disabled=${this.disabled}
            .required=${this.required}
            role="switch"
            aria-checked=${this.checked ? "true" : "false"}
            @click=${this.handleClick}
            @input=${this.handleInput}
            @invalid=${this.handleInvalid}
            @blur=${this.handleBlur}
            @focus=${this.handleFocus}
            @keydown=${this.handleKeyDown}
          />

          <span for="input" part="control" class="switch__control">
            <span part="thumb" class="switch__thumb"></span>
          </span>
        </label>

        ${this.divider ? this.renderDivider() : nothing}
      </div>
    `;
  }
}
FlowUiSwitch.styles = styles;
__decorateClass([
  query('input[type="checkbox"]')
], FlowUiSwitch.prototype, "input", 2);
__decorateClass([
  state()
], FlowUiSwitch.prototype, "hasFocus", 2);
__decorateClass([
  property()
], FlowUiSwitch.prototype, "label", 2);
__decorateClass([
  property()
], FlowUiSwitch.prototype, "name", 2);
__decorateClass([
  property()
], FlowUiSwitch.prototype, "value", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiSwitch.prototype, "size", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiSwitch.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiSwitch.prototype, "checked", 2);
__decorateClass([
  defaultValue("checked")
], FlowUiSwitch.prototype, "defaultChecked", 2);
__decorateClass([
  property({ reflect: true })
], FlowUiSwitch.prototype, "form", 2);
__decorateClass([
  property({ type: Boolean, reflect: true })
], FlowUiSwitch.prototype, "required", 2);
__decorateClass([
  property({ attribute: "help-text" })
], FlowUiSwitch.prototype, "helpText", 2);
__decorateClass([
  property({ type: Boolean })
], FlowUiSwitch.prototype, "divider", 2);
__decorateClass([
  watch("checked", { waitUntilFirstUpdate: true })
], FlowUiSwitch.prototype, "handleCheckedChange", 1);
__decorateClass([
  watch("disabled", { waitUntilFirstUpdate: true })
], FlowUiSwitch.prototype, "handleDisabledChange", 1);

export { FlowUiSwitch as default };
