import { hydrationSpecifier } from "./util.js";
class Metadata {
  constructor(filePathname, opts) {
    this.modules = opts.modules;
    this.hoisted = opts.hoisted;
    this.hydratedComponents = opts.hydratedComponents;
    this.hydrationDirectives = opts.hydrationDirectives;
    this.fileURL = new URL(filePathname, "http://example.com");
    this.metadataCache = new Map();
  }
  resolvePath(specifier) {
    return specifier.startsWith(".") ? new URL(specifier, this.fileURL).pathname : specifier;
  }
  getPath(Component) {
    const metadata = this.getComponentMetadata(Component);
    return (metadata == null ? void 0 : metadata.componentUrl) || null;
  }
  getExport(Component) {
    const metadata = this.getComponentMetadata(Component);
    return (metadata == null ? void 0 : metadata.componentExport) || null;
  }
  *hydratedComponentPaths() {
    const found = new Set();
    for (const metadata of this.deepMetadata()) {
      for (const component of metadata.hydratedComponents) {
        const path = metadata.getPath(component);
        if (path && !found.has(path)) {
          found.add(path);
          yield path;
        }
      }
    }
  }
  *hydrationDirectiveSpecifiers() {
    const found = new Set();
    for (const metadata of this.deepMetadata()) {
      for (const directive of metadata.hydrationDirectives) {
        if (!found.has(directive)) {
          found.add(directive);
          yield hydrationSpecifier(directive);
        }
      }
    }
  }
  *deepMetadata() {
    yield this;
    const seen = new Set();
    for (const { module: mod } of this.modules) {
      if (typeof mod.$$metadata !== "undefined") {
        const md = mod.$$metadata;
        for (const childMetdata of md.deepMetadata()) {
          if (!seen.has(childMetdata)) {
            seen.add(childMetdata);
            yield childMetdata;
          }
        }
      }
    }
  }
  getComponentMetadata(Component) {
    if (this.metadataCache.has(Component)) {
      return this.metadataCache.get(Component);
    }
    const metadata = this.findComponentMetadata(Component);
    this.metadataCache.set(Component, metadata);
    return metadata;
  }
  findComponentMetadata(Component) {
    const isCustomElement = typeof Component === "string";
    for (const { module, specifier } of this.modules) {
      const id = this.resolvePath(specifier);
      for (const [key, value] of Object.entries(module)) {
        if (isCustomElement) {
          if (key === "tagName" && Component === value) {
            return {
              componentExport: key,
              componentUrl: id
            };
          }
        } else if (Component === value) {
          return {
            componentExport: key,
            componentUrl: id
          };
        }
      }
    }
    return null;
  }
}
function createMetadata(filePathname, options) {
  return new Metadata(filePathname, options);
}
export {
  Metadata,
  createMetadata
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsiLi4vLi4vLi4vc3JjL3J1bnRpbWUvc2VydmVyL21ldGFkYXRhLnRzIl0sCiAgIm1hcHBpbmdzIjogIkFBQUE7QUFtQk8sZUFBZTtBQUFBLEVBU3JCLFlBQVksY0FBc0IsTUFBNkI7QUFDOUQsU0FBSyxVQUFVLEtBQUs7QUFDcEIsU0FBSyxVQUFVLEtBQUs7QUFDcEIsU0FBSyxxQkFBcUIsS0FBSztBQUMvQixTQUFLLHNCQUFzQixLQUFLO0FBQ2hDLFNBQUssVUFBVSxJQUFJLElBQUksY0FBYztBQUNyQyxTQUFLLGdCQUFnQixJQUFJO0FBQUE7QUFBQSxFQUcxQixZQUFZLFdBQTJCO0FBQ3RDLFdBQU8sVUFBVSxXQUFXLE9BQU8sSUFBSSxJQUFJLFdBQVcsS0FBSyxTQUFTLFdBQVc7QUFBQTtBQUFBLEVBR2hGLFFBQVEsV0FBK0I7QUFDdEMsVUFBTSxXQUFXLEtBQUsscUJBQXFCO0FBQzNDLFdBQU8sc0NBQVUsaUJBQWdCO0FBQUE7QUFBQSxFQUdsQyxVQUFVLFdBQStCO0FBQ3hDLFVBQU0sV0FBVyxLQUFLLHFCQUFxQjtBQUMzQyxXQUFPLHNDQUFVLG9CQUFtQjtBQUFBO0FBQUEsR0FPcEMseUJBQXlCO0FBQ3pCLFVBQU0sUUFBUSxJQUFJO0FBQ2xCLGVBQVcsWUFBWSxLQUFLLGdCQUFnQjtBQUMzQyxpQkFBVyxhQUFhLFNBQVMsb0JBQW9CO0FBQ3BELGNBQU0sT0FBTyxTQUFTLFFBQVE7QUFDOUIsWUFBSSxRQUFRLENBQUMsTUFBTSxJQUFJLE9BQU87QUFDN0IsZ0JBQU0sSUFBSTtBQUNWLGdC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