// @bun
var{defineProperty:F,getOwnPropertyNames:v,getOwnPropertyDescriptor:D}=Object,M=Object.prototype.hasOwnProperty;function b(j){return this[j]}var C=(j)=>{var z=(x??=new WeakMap).get(j),E;if(z)return z;if(z=F({},"__esModule",{value:!0}),j&&typeof j==="object"||typeof j==="function"){for(var H of v(j))if(!M.call(z,H))F(z,H,{get:b.bind(j,H),enumerable:!(E=D(j,H))||E.enumerable})}return x.set(j,z),z},x;var T=(j)=>j;function S(j,z){this[j]=T.bind(null,z)}var N=(j,z)=>{for(var E in z)F(j,E,{get:z[E],enumerable:!0,configurable:!0,set:S.bind(z,E)})};var h=(j,z)=>()=>(j&&(z=j(j=0)),z);var f=import.meta.require;function V(){return`
/**
 * Reactive mixin for stx web components
 * Adds reactive state management to custom elements
 */
function StxReactive(Base) {
  return class extends Base {
    #state = {};
    #watchers = new Map();
    #computed = new Map();
    #dirty = new Set();
    #updateScheduled = false;

    constructor() {
      super();
      this.#initializeState();
    }

    #initializeState() {
      // Initialize reactive properties from static definition
      const props = this.constructor.reactiveProperties || {};
      for (const [name, config] of Object.entries(props)) {
        this.#state[name] = config.default;
        this.#defineProperty(name, config);
      }
    }

    #defineProperty(name, config) {
      Object.defineProperty(this, name, {
        get: () => this.#state[name],
        set: (value) => {
          const oldValue = this.#state[name];
          if (oldValue !== value) {
            this.#state[name] = value;
            this.#markDirty(name);
            this.#notifyWatchers(name, value, oldValue);
            if (config.reflect) {
              this.#reflectAttribute(name, value);
            }
          }
        },
        enumerable: true,
        configurable: true
      });
    }

    #reflectAttribute(name, value) {
      const attrName = name.replace(/([A-Z])/g, '-$1').toLowerCase();
      if (value === null || value === undefined || value === false) {
        this.removeAttribute(attrName);
      }
else if (value === true) {
        this.setAttribute(attrName, '');
      }
else {
        this.setAttribute(attrName, String(value));
      }
    }

    #markDirty(name) {
      this.#dirty.add(name);
      this.#scheduleUpdate();
    }

    #scheduleUpdate() {
      if (!this.#updateScheduled) {
        this.#updateScheduled = true;
        queueMicrotask(() => {
          this.#updateScheduled = false;
          this.#performUpdate();
        });
      }
    }

    #performUpdate() {
      // Recompute computed properties that depend on dirty state
      for (const [name, { deps, getter }] of this.#computed) {
        if (deps.some(dep => this.#dirty.has(dep))) {
          this.#state[name] = getter.call(this);
        }
      }

      // Clear dirty set and trigger re-render
      this.#dirty.clear();
      if (typeof this.render === 'function') {
        this.render();
      }
    }

    #notifyWatchers(name, newValue, oldValue) {
      const handlers = this.#watchers.get(name) || [];
      for (const handler of handlers) {
        try {
          handler.call(this, newValue, oldValue);
        }
catch (e) {
          console.error(\`Watcher error for "\${name}":\`, e);
        }
      }
    }

    $watch(property, handler, options = {}) {
      if (!this.#watchers.has(property)) {
        this.#watchers.set(property, []);
      }
      this.#watchers.get(property).push(handler);

      if (options.immediate) {
        handler.call(this, this.#state[property], undefined);
      }

      // Return unwatch function
      return () => {
        const handlers = this.#watchers.get(property);
        const index = handlers.indexOf(handler);
        if (index > -1) {
          handlers.splice(index, 1);
        }
      };
    }

    $computed(name, deps, getter) {
      this.#computed.set(name, { deps, getter });
      Object.defineProperty(this, name, {
        get: () => {
          if (!(name in this.#state)) {
            this.#state[name] = getter.call(this);
          }
          return this.#state[name];
        },
        enumerable: true,
        configurable: true
      });
    }

    $set(property, value) {
      this[property] = value;
    }

    $batch(fn) {
      // Temporarily disable updates during batch
      const prevScheduled = this.#updateScheduled;
      this.#updateScheduled = true;
      try {
        fn();
      }
finally {
        this.#updateScheduled = prevScheduled;
        if (!prevScheduled) {
          this.#scheduleUpdate();
        }
      }
    }
  };
}
`}function u(j,z,E={}){let{properties:H=[],computed:O=[],watchers:P=[],methods:Q={},lifecycle:J={}}=E,B=j.name,U=j.tag,L=j.shadowDOM!==!1,W=H.map((q)=>{let k=q.default!==void 0?JSON.stringify(q.default):R(q.type);return`    ${q.name}: { type: '${q.type}', default: ${k}, reflect: ${!!q.reflect} }`}).join(`,
`),X=O.map((q)=>{let k=JSON.stringify(q.dependencies);return`    this.$computed('${q.name}', ${k}, ${q.getter});`}).join(`
`),Y=P.map((q)=>{let k=q.immediate||q.deep?`, { immediate: ${!!q.immediate}, deep: ${!!q.deep} }`:"";return`    this.$watch('${q.property}', ${q.handler}${k});`}).join(`
`),Z=Object.entries(Q).map(([q,k])=>`  ${q}${k}`).join(`

`),_=J.connected||"",$=J.disconnected||"",G=J.adopted||"",I=J.attributeChanged||"",A=z.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$/g,"\\$");return`
${V()}

class ${B} extends StxReactive(HTMLElement) {
  static reactiveProperties = {
${W}
  };

  static get observedAttributes() {
    return ${JSON.stringify(j.attributes||[])};
  }

  #shadowRoot;

  constructor() {
    super();
    ${L?"this.#shadowRoot = this.attachShadow({ mode: 'open' });":""}
    this.#setupComputed();
    this.#setupWatchers();
  }

  #setupComputed() {
${X}
  }

  #setupWatchers() {
${Y}
  }

  connectedCallback() {
    this.render();
    ${_}
  }

  disconnectedCallback() {
    ${$}
  }

  adoptedCallback() {
    ${G}
  }

  attributeChangedCallback(name, oldValue, newValue) {
    // Convert attribute name to property name (kebab-case to camelCase)
    const propName = name.replace(/-([a-z])/g, (_, c) => c.toUpperCase());

    // Get the property type
    const propConfig = ${B}.reactiveProperties[propName];
    if (propConfig) {
      let value = newValue;

      // Convert based on type
      switch (propConfig.type) {
        case 'number':
          value = newValue !== null ? Number(newValue) : propConfig.default;
          break;
        case 'boolean':
          value = newValue !== null;
          break;
        case 'object':
        case 'array':
          try {
            value = newValue !== null ? JSON.parse(newValue) : propConfig.default;
          }
catch (e) {
            value = propConfig.default;
          }
          break;
      }

      this[propName] = value;
    }

    ${I}
  }

  render() {
    const root = ${L?"this.#shadowRoot":"this"};
    root.innerHTML = this.#template();
  }

  #template() {
    return \`${A}\`;
  }

${Z}
}

customElements.define('${U}', ${B});

export { ${B} };
`}function w(j,z,E={}){let{properties:H=[],computed:O=[],watchers:P=[],methods:Q={},lifecycle:J={}}=E,B=j.name,U=j.tag,L=j.shadowDOM!==!1,W=H.map((k)=>{let K=g(k.type);return`  ${k.name}: ${K};`}).join(`
`),X=H.map((k)=>{let K=k.default!==void 0?JSON.stringify(k.default):R(k.type);return`    ${k.name}: { type: '${k.type}' as const, default: ${K}, reflect: ${!!k.reflect} }`}).join(`,
`),Y=O.map((k)=>{let K=JSON.stringify(k.dependencies);return`    this.$computed('${k.name}', ${K}, ${k.getter});`}).join(`
`),Z=P.map((k)=>{let K=k.immediate||k.deep?`, { immediate: ${!!k.immediate}, deep: ${!!k.deep} }`:"";return`    this.$watch('${k.property}', ${k.handler}${K});`}).join(`
`),_=Object.entries(Q).map(([k,K])=>`  ${k}${K}`).join(`

`),$=J.connected||"",G=J.disconnected||"",I=J.adopted||"",A=J.attributeChanged||"",q=z.replace(/\\/g,"\\\\").replace(/`/g,"\\`").replace(/\$/g,"\\$");return`
interface ${B}Props {
${W}
}

interface ReactivePropertyConfig {
  type: 'string' | 'number' | 'boolean' | 'object' | 'array';
  default: unknown;
  reflect: boolean;
}

interface ReactiveElement extends HTMLElement {
  $watch(property: string, handler: (newVal: unknown, oldVal: unknown) => void, options?: { immediate?: boolean; deep?: boolean }): () => void;
  $computed(name: string, deps: string[], getter: () => unknown): void;
  $set(property: string, value: unknown): void;
  $batch(fn: () => void): void;
}

${V()}

class ${B} extends StxReactive(HTMLElement) implements ReactiveElement {
  static reactiveProperties: Record<string, ReactivePropertyConfig> = {
${X}
  };

  static get observedAttributes(): string[] {
    return ${JSON.stringify(j.attributes||[])};
  }

  #shadowRoot: ShadowRoot | null = null;

  constructor() {
    super();
    ${L?"this.#shadowRoot = this.attachShadow({ mode: 'open' });":""}
    this.#setupComputed();
    this.#setupWatchers();
  }

  #setupComputed(): void {
${Y}
  }

  #setupWatchers(): void {
${Z}
  }

  connectedCallback(): void {
    this.render();
    ${$}
  }

  disconnectedCallback(): void {
    ${G}
  }

  adoptedCallback(): void {
    ${I}
  }

  attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void {
    const propName = name.replace(/-([a-z])/g, (_: string, c: string) => c.toUpperCase());

    const propConfig = ${B}.reactiveProperties[propName];
    if (propConfig) {
      let value: unknown = newValue;

      switch (propConfig.type) {
        case 'number':
          value = newValue !== null ? Number(newValue) : propConfig.default;
          break;
        case 'boolean':
          value = newValue !== null;
          break;
        case 'object':
        case 'array':
          try {
            value = newValue !== null ? JSON.parse(newValue) : propConfig.default;
          }
catch {
            value = propConfig.default;
          }
          break;
      }

      (this as Record<string, unknown>)[propName] = value;
    }

    ${A}
  }

  render(): void {
    const root = ${L?"this.#shadowRoot":"this"};
    if (root) {
      root.innerHTML = this.#template();
    }
  }

  #template(): string {
    return \`${q}\`;
  }

${_}
}

customElements.define('${U}', ${B});

export { ${B} };
export type { ${B}Props };
`}function R(j){switch(j){case"string":return"''";case"number":return"0";case"boolean":return"false";case"array":return"[]";case"object":return"{}";default:return"undefined"}}function g(j){switch(j){case"string":return"string";case"number":return"number";case"boolean":return"boolean";case"array":return"unknown[]";case"object":return"Record<string, unknown>";default:return"unknown"}}function l(){return`
(function() {
  'use strict';

  // StxReactive mixin for dynamic component creation
  window.StxReactive = ${V().replace(/\n/g," ").replace(/\s+/g," ")};

  // Helper to create reactive components dynamically
  window.createReactiveComponent = function(tag, config) {
    const { properties = {}, template, methods = {}, lifecycle = {} } = config;

    class DynamicComponent extends StxReactive(HTMLElement) {
      static reactiveProperties = properties;
      static get observedAttributes() {
        return Object.keys(properties).map(p =>
          p.replace(/([A-Z])/g, '-$1').toLowerCase()
        );
      }

      constructor() {
        super();
        if (config.shadow !== false) {
          this.attachShadow({ mode: 'open' });
        }
      }

      connectedCallback() {
        this.render();
        if (lifecycle.connected) lifecycle.connected.call(this);
      }

      disconnectedCallback() {
        if (lifecycle.disconnected) lifecycle.disconnected.call(this);
      }

      render() {
        const root = this.shadowRoot || this;
        root.innerHTML = typeof template === 'function'
          ? template.call(this)
          : template;
      }
    }

    // Add methods
    Object.entries(methods).forEach(([name, fn]) => {
      DynamicComponent.prototype[name] = fn;
    });

    customElements.define(tag, DynamicComponent);
    return DynamicComponent;
  };
})();
`}export{l as generateReactiveRuntime,V as generateReactiveMixin,w as generateReactiveComponentTS,u as generateReactiveComponentJS};
