var __defProp = Object.defineProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {
      get: all[name],
      enumerable: true,
      configurable: true,
      set: (newValue) => all[name] = () => newValue
    });
};
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);

// src/resolver.ts
class ComponentResolver {
  cache = new Map;
  options;
  constructor(options = {}) {
    this.options = {
      baseDir: options.baseDir || process.cwd(),
      cache: options.cache ?? true,
      customFetch: options.customFetch || this.defaultFetch.bind(this)
    };
  }
  async resolve(importStmt) {
    const sourcePath = this.getSourcePath(importStmt.source);
    if (this.options.cache && this.cache.has(sourcePath)) {
      const cached = this.cache.get(sourcePath);
      return this.extractComponents(importStmt.clause, cached);
    }
    let component;
    if (importStmt.source.type === "local") {
      component = await this.resolveLocal(importStmt.source.path);
    } else {
      component = await this.resolveUrl(importStmt.source.url);
    }
    if (this.options.cache) {
      this.cache.set(sourcePath, component);
    }
    return this.extractComponents(importStmt.clause, component);
  }
  async resolveLocal(path) {
    throw new Error(`Dynamic local component resolution not yet implemented: ${path}
` + `Please use the build-components.ts script to generate component imports.`);
  }
  async resolveUrl(url) {
    try {
      const response = await this.options.customFetch(url);
      const data = JSON.parse(response);
      if (!data.module || !data.template) {
        throw new Error(`Invalid component format from ${url}. Expected { module, template }`);
      }
      return data;
    } catch (error) {
      throw new Error(`Failed to resolve component from ${url}: ${error instanceof Error ? error.message : String(error)}`);
    }
  }
  async defaultFetch(url) {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    return response.text();
  }
  extractComponents(clause, component) {
    if (clause.type === "default") {
      return {
        [clause.name]: component
      };
    } else {
      const result = {};
      for (const name of clause.names) {
        result[name] = component;
      }
      return result;
    }
  }
  getSourcePath(source) {
    return source.type === "local" ? source.path : source.url;
  }
  clearCache() {
    this.cache.clear();
  }
  static parseImports(text) {
    const imports = [];
    const importRegex = /import\s+(?:(\{[^}]*\})|(\w+))\s+from\s+["']([^"']+)["']/g;
    let match;
    while ((match = importRegex.exec(text)) !== null) {
      const [, namedImports, defaultImport, source] = match;
      if (!source)
        continue;
      let clause;
      if (namedImports) {
        const names = namedImports.slice(1, -1).split(",").map((n) => n.trim()).filter((n) => n.length > 0);
        clause = { type: "named", names };
      } else if (defaultImport) {
        clause = { type: "default", name: defaultImport };
      } else {
        continue;
      }
      const sourceObj = source.startsWith("http://") || source.startsWith("https://") ? { type: "url", url: source } : { type: "local", path: source };
      imports.push({ clause, source: sourceObj });
    }
    return imports;
  }
}
export {
  ComponentResolver
};

//# debugId=CD36667EA0171D3D64756E2164756E21
