/**
 * Non-side-effect class export for `<blockquote-ui>`.
 *
 * Importing this file gives you the class without auto-registering the
 * tag. Useful for test isolation, subclassing with tag-name override,
 * or selective composition.
 *
 * The auto-register path stays at `@adia-ai/web-components/components/blockquote`
 * (which imports this file + calls `defineIfFree()`).
 *
 * @see ../../USAGE.md#registration--auto-vs-explicit
 */

/**
 * <blockquote-ui cite="Alan Kay">
 *   The best way to predict the future is to invent it.
 * </blockquote-ui>
 *
 * Layout:
 *   │ italic quote body                              ← left rule indicator
 *   │ — citation line                                ← em-dash prefix
 *
 * Two attribution paths:
 *   • [cite="..."] attribute → renders a small em-dashed line below
 *   • <span slot="cite">...</span> slot → richer attribution (links, etc.)
 *     Slot content overrides the attribute when both are present.
 */

import { UIElement } from '../../core/element.js';

export class UIBlockquote extends UIElement {
  static properties = {
    cite: { type: String, default: '', reflect: true },
  };

  static template = () => null;

  connected() {
    // Implicit semantic role — the tag name carries blockquote meaning,
    // but assistive tech doesn't infer from custom-element tag names.
    if (!this.hasAttribute('role')) this.setAttribute('role', 'blockquote');
  }

  render() {
    // Stamp the citation line from [cite] only when no [slot="cite"]
    // content is provided. Slot wins on conflict (per the contract).
    const slotted = this.querySelector('[slot="cite"]:not([data-cite-stamped])');
    const stamped = this.querySelector('[slot="cite"][data-cite-stamped]');
    if (slotted) {
      // Author supplied richer attribution — drop any stamped version.
      stamped?.remove();
      return;
    }
    if (this.cite) {
      if (stamped) {
        stamped.textContent = this.cite;
      } else {
        const el = document.createElement('span');
        el.setAttribute('slot', 'cite');
        el.setAttribute('data-cite-stamped', '');
        el.textContent = this.cite;
        this.appendChild(el);
      }
    } else if (stamped) {
      stamped.remove();
    }
  }
}
