import {
  prefetchBlockData
} from "./chunk-7DS4Q3GA.mjs";

// src/rendering/helpers/loadContent.ts
function isPageContent(result) {
  return result.type === "page";
}
function isEntryContent(result) {
  return result.type === "entry";
}
async function loadContent(params) {
  const { client, siteId, path, preview = false } = params;
  const [site, contentResponse] = await Promise.all([
    client.getSite({ id: siteId }),
    client.getPage({ siteId, path, preview })
  ]);
  if (isEntryResponse(contentResponse)) {
    const entryData = contentResponse.entry;
    const entry = {
      id: entryData.id,
      type: entryData.type,
      title: entryData.title,
      slug: entryData.slug,
      path: entryData.path,
      status: entryData.status,
      publishAt: entryData.publishAt,
      // Use draft content in preview mode, otherwise use published content
      content: preview ? entryData.draftContent ?? entryData.content : entryData.content,
      metaTitle: preview ? entryData.draftMetaTitle ?? entryData.metaTitle : entryData.metaTitle,
      metaDescription: preview ? entryData.draftMetaDescription ?? entryData.metaDescription : entryData.metaDescription,
      createdAt: entryData.createdAt,
      updatedAt: entryData.updatedAt
    };
    const { templatePage, resolvedData: resolvedData2 } = await processEntryTemplate(
      contentResponse.templates,
      entry,
      { siteId, preview },
      client
    );
    return {
      type: "entry",
      entry,
      templatePage,
      resolvedData: resolvedData2,
      dataContext: { contentEntry: entry.content },
      theme: site.theme,
      siteId
    };
  }
  const { page: pageData } = contentResponse;
  const blocks = pageData.blocks.map((block) => validateAndConvertBlock(block, "page"));
  const pageOutline = {
    name: pageData.name,
    path: pageData.path,
    purpose: pageData.purpose,
    blocks
  };
  const resolvedData = await prefetchBlockData(
    pageOutline,
    {
      siteId,
      pageId: pageData.id,
      previewStage: preview ? "preview" : "published"
    },
    client
  );
  return {
    type: "page",
    page: pageOutline,
    theme: site.theme,
    siteId,
    resolvedData
  };
}
function isEntryResponse(response) {
  return "type" in response && response.type === "entry";
}
function validateAndConvertBlock(block, source) {
  if (!block || typeof block !== "object") {
    throw new Error(`Invalid block format in ${source} API response`);
  }
  const blockRecord = block;
  const kindField = source === "template" ? "blockKind" : "kind";
  const kindValue = blockRecord[kindField];
  if (typeof blockRecord.id !== "string" && blockRecord.id !== null) {
    throw new Error(`Invalid block id in ${source}: expected string or null, got ${typeof blockRecord.id}`);
  }
  if (typeof kindValue !== "string") {
    throw new Error(`Invalid block ${kindField} in ${source}: expected string, got ${typeof kindValue}`);
  }
  if (source === "page") {
    if (typeof blockRecord.purpose !== "string") {
      throw new Error(`Invalid block purpose in ${source}: expected string, got ${typeof blockRecord.purpose}`);
    }
    return {
      id: blockRecord.id,
      kind: kindValue,
      purpose: blockRecord.purpose
    };
  }
  const scope = blockRecord.scope;
  const content = blockRecord.content ?? {};
  return {
    id: blockRecord.id,
    kind: kindValue,
    purpose: scope === "entry" ? "entry-content" : "template-layout",
    content
  };
}
async function processEntryTemplate(templates, entry, context, client) {
  const template = templates?.[0];
  if (!template || !template.blocks?.length) {
    return { templatePage: null, resolvedData: {} };
  }
  const blocks = template.blocks.map((block) => validateAndConvertBlock(block, "template"));
  const templatePage = {
    name: template.name || "Entry Template",
    path: entry.path || "/",
    purpose: "entry-template",
    blocks
  };
  const resolvedData = await prefetchBlockData(
    templatePage,
    {
      siteId: context.siteId,
      pageId: template.id,
      previewStage: context.preview ? "preview" : "published"
    },
    client
  );
  return { templatePage, resolvedData };
}

export {
  isPageContent,
  isEntryContent,
  loadContent
};
//# sourceMappingURL=chunk-OP2GHK27.mjs.map