function collectRefsFromField(fieldName, field) {
  if (field.type === "component" && field.component) {
    return [{ field: fieldName, component: field.component, kind: "component" }];
  }
  if (field.type === "dynamicZone" && Array.isArray(field.components)) {
    return field.components.map((component) => ({
      field: fieldName,
      component,
      kind: "dynamicZone"
    }));
  }
  return [];
}
function collectComponentRefsFromSchema(schema) {
  const refs = [];
  for (const [fieldName, field] of Object.entries(schema.attributes)) {
    refs.push(...collectRefsFromField(fieldName, field));
  }
  return refs;
}
function collectComponentRefsFromComponentSchema(component) {
  const refs = [];
  for (const [fieldName, field] of Object.entries(component.attributes)) {
    refs.push(...collectRefsFromField(fieldName, field));
  }
  return refs;
}
function findComponentUsages(componentUid, schemas, components) {
  const schemaUsages = [];
  const componentUsages = [];
  for (const schema of schemas) {
    const refs = collectComponentRefsFromSchema(schema).filter(
      (ref) => ref.component === componentUid
    );
    if (refs.length > 0) {
      schemaUsages.push({ name: schema.info.singularName, refs });
    }
  }
  for (const component of components) {
    if (component.uid === componentUid) {
      continue;
    }
    const refs = collectComponentRefsFromComponentSchema(component).filter(
      (ref) => ref.component === componentUid
    );
    if (refs.length > 0) {
      componentUsages.push({ name: component.uid, refs });
    }
  }
  return { schemas: schemaUsages, components: componentUsages };
}

export { collectComponentRefsFromSchema as c, findComponentUsages as f };
//# sourceMappingURL=component-usage.mjs.map
