/**
 * Custom-element registration helpers.
 *
 * The package's components auto-register their tags at the bottom of each
 * component `.js` file (the de-facto web-components convention). These
 * helpers give consumers controllable alternatives:
 *
 * - `register(tagName, ctor)` — direct alias for `customElements.define`.
 *   No magic; provided so consumers can write the registration call without
 *   importing the constructor globally.
 *
 * - `defineIfFree(tagName, ctor)` — no-op if the tag is already registered.
 *   Use this when multiple packages may try to register the same tag (e.g.
 *   when a host page imports the AdiaUI bundle AND a downstream consumer
 *   also imports per-component subpaths). Without this, the second
 *   `customElements.define` throws `NotSupportedError`.
 *
 * @see ../USAGE.md#registration--auto-vs-explicit
 */

/**
 * Register a custom element. Same semantics as `customElements.define`;
 * throws if `tagName` is already registered.
 *
 * @param {string} tagName  The kebab-case tag name (e.g. `'my-slider'`)
 * @param {CustomElementConstructor} ctor  The class to register
 */
export function register(tagName, ctor) {
  customElements.define(tagName, ctor);
}

/**
 * Register a custom element only if the tag isn't already taken. Useful for
 * conflict-safe registration when multiple sources may want to register the
 * same tag.
 *
 * Returns `true` if registration happened, `false` if the tag was already
 * registered.
 *
 * @param {string} tagName  The kebab-case tag name
 * @param {CustomElementConstructor} ctor  The class to register
 * @returns {boolean}
 */
export function defineIfFree(tagName, ctor) {
  if (customElements.get(tagName)) return false;
  customElements.define(tagName, ctor);
  return true;
}

/**
 * Check whether a tag is already registered.
 *
 * @param {string} tagName
 * @returns {boolean}
 */
export function isRegistered(tagName) {
  return Boolean(customElements.get(tagName));
}
