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 { h } from '@stencil/core';
import { MapToComponentField, VegaSlimmer } from 'vega-slimmer/core';
export class VegaSelectionTileRenderer extends VegaSlimmer {
  constructor() {
    super(...arguments);
    /**
     * Activation handler invoked from the host's `@Listen('click')` listener
     * (registered on the host element itself). The host carries
     * `role="radio"`/`"checkbox"`, `tabindex`, and the activation listeners,
     * so this is the only interactive node per tile. VoiceOver's VO+Space
     * dispatches a synthetic click on the focused host, which reaches this
     * function through `VegaSelectionTile.onHostClick()`.
     */
    this.activate = () => {
      if (this.disabled) {
        return;
      }
      if (!(this.selectType === 'radio' && this.checked)) {
        this.checkedController.onCheck(!this.checked);
      }
    };
    this.handleTileKeydown = (event) => {
      if (this.disabled) {
        return;
      }
      // eslint-disable-next-line spellcheck/spell-checker
      if (event.key === 'Enter' || event.key === ' ' || event.key === 'Spacebar') {
        event.preventDefault();
        this.activate();
      }
    };
  }
  getSelectorContainerRef() {
    return this.selectorContainerRef;
  }
  // Accessible name for the host's `aria-label`. Public because the
  // component renders the host and needs to call this to compute the
  // host's `aria-label` value (see `VegaSelectionTile.render`).
  //
  // Fallback chain: titleText (+ note) → trimmed host light-DOM text
  // (covers `content`/`prefix` slots) → `value`. `note` is appended so
  // VoiceOver announces both the title and the supporting line
  // (e.g. "Basic Plan, $9 / month") when traversing tiles with VO+→/VO+←;
  // the visual subtree is `aria-hidden`, so without this the note would
  // never reach the accessibility tree.
  //
  // The parent group's label is intentionally NOT prepended here: the
  // group renderer exposes its label as an independently focusable AT
  // stop, so duplicating it on every tile would make AT users hear the
  // group label N+1 times.
  getAccessibleName() {
    if (this.titleText) {
      return this.note ? `${this.titleText}, ${this.note}` : this.titleText;
    }
    // Prefer `innerText` so adjacent inline/block elements are separated
    // by whitespace (e.g. `<div>Pro Plan<span>Popular</span></div>` reads
    // as "Pro Plan Popular" instead of "Pro PlanPopular" from raw
    // `textContent` concatenation). Fall back to `textContent` for
    // environments (e.g. jsdom) that do not implement `innerText`, and
    // to `''` to satisfy the `string | null` type of `textContent`.
    const raw = this.host.innerText || this.host.textContent || '';
    const slotText = raw.replace(/\s+/g, ' ').trim();
    return slotText || this.value || '';
  }
  render() {
    // ARIA semantics (role/aria-*), focus (tabindex), and activation
    // handlers live on the COMPONENT HOST (see `VegaSelectionTile.render`
    // and the `@Listen('click')`/`@Listen('keydown')` methods on the
    // component class), not on this inner element. Keeping role + tabindex
    // off this element is what prevents Firefox/VoiceOver from triple-
    // announcing each tile (host group wrapper + inner role=radio + host
    // close). This element is now purely a layout/styling wrapper.
    return (h("vega-flex", { "align-items": "start", "justify-content": "start", gap: "size-16", class: {
        'selection-tile': true,
        'disabled': this.disabled,
        'checked': this.checked,
        'error': this.showError,
      } },
      h("div", { class: "vega-flex-grow", "aria-hidden": "true" }, this.renderContent()),
      this.renderSelect()));
  }
  renderContent() {
    if (this.hasContentSlot()) {
      return h("slot", { name: "content" });
    }
    else {
      return (h("vega-flex", { gap: "size-12" },
        h("vega-flex", { class: "vega-flex-shrink-0" }, this.renderPrefixIcon()),
        h("div", { class: "vega-flex-grow" }, this.renderText())));
    }
  }
  renderPrefixIcon() {
    if (this.hasPrefixSlot()) {
      return h("slot", { name: "prefix" });
    }
    else if (this.prefixIcon) {
      return (h("vega-icon", { icon: this.prefixIcon, size: "size-16", color: this.disabled ? 'text-secondary' : 'text-primary', class: "selection-tile-icon" }));
    }
  }
  renderText() {
    return (h("vega-flex", { class: "vega-flex-grow", direction: "col", "align-items": "start", "justify-content": "start" },
      h("div", { class: "selection-tile-title" }, this.titleText),
      this.note && h("div", { class: "selection-tile-note" }, this.note)));
  }
  renderSelect() {
    return (h("vega-flex", Object.assign({ "align-items": "start", class: "vega-flex-shrink-0 selection-tile-select", "aria-hidden": "true" }, { inert: true }, { ref: (ref) => (this.selectorContainerRef = ref) }), this.selectType === 'checkbox' ? this.renderCheckbox() : this.renderRadio()));
  }
  renderCheckbox() {
    return h("vega-checkbox", { size: "small", disabled: this.disabled, checked: this.checked });
  }
  renderRadio() {
    return h("vega-radio", { size: "small", disabled: this.disabled, checked: this.checked });
  }
  hasPrefixSlot() {
    return this.host.querySelector(`[slot=prefix]`) !== null;
  }
  hasContentSlot() {
    return this.host.querySelector(`[slot=content]`) !== null;
  }
}
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "host", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "prefixIcon", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "titleText", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "note", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "selectType", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "disabled", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "checked", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "showError", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "value", void 0);
__decorate([
  MapToComponentField()
], VegaSelectionTileRenderer.prototype, "checkedController", void 0);
