import { entityRegistry, ensureInitialized } from "../../entities/registry";
import { existsSync } from "fs";
import { join } from "path";
async function resolvePublicEntityFromUrl(pathname) {
  var _a, _b, _c, _d, _e;
  const segments = pathname.split("/").filter(Boolean);
  if (segments.length === 0) {
    return {
      entityName: "",
      entityConfig: null,
      hasCustomOverride: false,
      isValidPublicEntity: false,
      hasArchivePage: false,
      hasSinglePage: false
    };
  }
  const entitySlug = segments[0];
  await ensureInitialized();
  const entityConfig = entityRegistry.getBySlug(entitySlug);
  if (!entityConfig) {
    return {
      entityName: entitySlug,
      entityConfig: null,
      hasCustomOverride: false,
      isValidPublicEntity: false,
      hasArchivePage: false,
      hasSinglePage: false
    };
  }
  const isPublicEntity = ((_a = entityConfig.access) == null ? void 0 : _a.public) && entityConfig.enabled;
  if (!isPublicEntity) {
    return {
      entityName: entityConfig.slug,
      entityConfig,
      hasCustomOverride: false,
      isValidPublicEntity: false,
      hasArchivePage: false,
      hasSinglePage: false
    };
  }
  const hasCustomOverride = await checkPublicCustomOverride(entitySlug);
  return {
    entityName: entityConfig.slug,
    entityConfig,
    hasCustomOverride,
    isValidPublicEntity: true,
    hasArchivePage: ((_c = (_b = entityConfig.ui) == null ? void 0 : _b.public) == null ? void 0 : _c.hasArchivePage) ?? false,
    hasSinglePage: ((_e = (_d = entityConfig.ui) == null ? void 0 : _d.public) == null ? void 0 : _e.hasSinglePage) ?? false
  };
}
async function checkPublicCustomOverride(entityName) {
  try {
    const customListPath = join(
      process.cwd(),
      "app",
      "(public)",
      "(contents)",
      entityName,
      "page.tsx"
    );
    return existsSync(customListPath);
  } catch (error) {
    console.warn(`Error checking public custom override for ${entityName}:`, error);
    return false;
  }
}
async function getPublicEntities() {
  await ensureInitialized();
  const allEntities = entityRegistry.getAll();
  return allEntities.filter(
    (entity) => {
      var _a, _b, _c, _d, _e;
      return entity.enabled && ((_a = entity.access) == null ? void 0 : _a.public) && (((_c = (_b = entity.ui) == null ? void 0 : _b.public) == null ? void 0 : _c.hasArchivePage) || ((_e = (_d = entity.ui) == null ? void 0 : _d.public) == null ? void 0 : _e.hasSinglePage));
    }
  );
}
function validatePublicEntityPage(entityConfig, pageType) {
  var _a, _b, _c, _d, _e;
  if (!((_a = entityConfig.access) == null ? void 0 : _a.public) || !entityConfig.enabled) {
    return false;
  }
  switch (pageType) {
    case "archive":
      return ((_c = (_b = entityConfig.ui) == null ? void 0 : _b.public) == null ? void 0 : _c.hasArchivePage) ?? false;
    case "single":
      return ((_e = (_d = entityConfig.ui) == null ? void 0 : _d.public) == null ? void 0 : _e.hasSinglePage) ?? false;
    default:
      return false;
  }
}
export {
  getPublicEntities,
  resolvePublicEntityFromUrl,
  validatePublicEntityPage
};
