import { LitElement, html } from "lit";
import { live } from "lit/directives/live.js";
import { property } from "lit/decorators.js";
import { customElement } from "../../decorator.js";
import radioStyles from "./radio.css.js";
import { classMap } from "lit/directives/class-map.js";
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;
};
let ObcRadio = class extends LitElement {
  constructor() {
    super(...arguments);
    this.checked = false;
    this.disabled = false;
    this.required = false;
    this.inputId = "";
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    const root = this.getRootNode();
    if (root instanceof ShadowRoot) {
      ObcRadio.adoptedRoots.delete(root);
    }
  }
  connectedCallback() {
    super.connectedCallback();
    this.injectStyles();
  }
  injectStyles() {
    this.injectGlobalStyles();
    this.injectIntoShadowDOM();
  }
  injectGlobalStyles() {
    if (ObcRadio.stylesInjected) return;
    const styleId = "obc-radio-global-styles";
    if (document.head.querySelector(`#${styleId}`)) {
      ObcRadio.stylesInjected = true;
      return;
    }
    const style = document.createElement("style");
    style.id = styleId;
    style.textContent = this.getCSSText();
    document.head.appendChild(style);
    ObcRadio.stylesInjected = true;
  }
  injectIntoShadowDOM() {
    const root = this.getRootNode();
    if (root instanceof ShadowRoot) {
      this.adoptStylesIntoShadowRoot(root);
    } else if (root === document) {
      let element = this.parentElement;
      while (element) {
        if (element.shadowRoot) {
          this.adoptStylesIntoShadowRoot(element.shadowRoot);
          break;
        }
        element = element.parentElement;
      }
    }
  }
  adoptStylesIntoShadowRoot(shadowRoot) {
    if (ObcRadio.adoptedRoots.has(shadowRoot)) return;
    const style = document.createElement("style");
    style.textContent = this.getCSSText();
    shadowRoot.appendChild(style);
    ObcRadio.adoptedRoots.add(shadowRoot);
  }
  getCSSText() {
    if (typeof radioStyles === "string") {
      return radioStyles;
    } else if (radioStyles && typeof radioStyles === "object" && "cssText" in radioStyles) {
      return radioStyles.cssText;
    }
    return String(radioStyles);
  }
  createRenderRoot() {
    return this;
  }
  onClick() {
    if (this.disabled) {
      return;
    }
    this.renderRoot.querySelector("input")?.click();
  }
  onChange(event) {
    event.stopPropagation();
    const target = event.target;
    this.dispatchEvent(
      new CustomEvent("change", {
        detail: { value: target.value, checked: target.checked }
      })
    );
  }
  render() {
    if (this.label !== void 0) {
      const labelClasses = {
        "has-label": true,
        "obc-radio-button": true,
        disabled: this.disabled
      };
      return html`
        <label
          for=${this.inputId}
          class=${classMap(labelClasses)}
          @click=${this.onClick}
        >
          <input
            type="radio"
            .name=${this.name || ""}
            .value=${this.value || ""}
            .id=${this.inputId}
            ?checked=${live(this.checked)}
            ?disabled=${this.disabled}
            ?required=${this.required}
            @change=${this.onChange}
          />
          <span class="label">${this.label}</span>
        </label>
      `;
    } else {
      return html`
        <input
          class="obc-radio-button"
          type="radio"
          .name=${this.name || ""}
          .value=${this.value || ""}
          .id=${this.inputId}
          ?checked=${live(this.checked)}
          ?disabled=${this.disabled}
          ?required=${this.required}
          @change=${this.onChange}
        />
      `;
    }
  }
};
ObcRadio.stylesInjected = false;
ObcRadio.adoptedRoots = /* @__PURE__ */ new Set();
__decorateClass([
  property({ type: String })
], ObcRadio.prototype, "label", 2);
__decorateClass([
  property({ type: String })
], ObcRadio.prototype, "name", 2);
__decorateClass([
  property({ type: String })
], ObcRadio.prototype, "value", 2);
__decorateClass([
  property({ type: Boolean })
], ObcRadio.prototype, "checked", 2);
__decorateClass([
  property({ type: Boolean })
], ObcRadio.prototype, "disabled", 2);
__decorateClass([
  property({ type: Boolean })
], ObcRadio.prototype, "required", 2);
__decorateClass([
  property({ type: String })
], ObcRadio.prototype, "inputId", 2);
ObcRadio = __decorateClass([
  customElement("obc-radio")
], ObcRadio);
export {
  ObcRadio
};
//# sourceMappingURL=radio.js.map
