"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }


var _chunkG4CKM4ENjs = require('./chunk-G4CKM4EN.js');


var _chunkHOY77YBFjs = require('./chunk-HOY77YBF.js');

// src/rendering/helpers/loadPage.ts
async function loadPage(params) {
  const { client, siteId, path, pageId, preview = false, dataLoaderOverrides } = params;
  const [site, pageResponse] = await Promise.all([
    client.getSite({ id: siteId }),
    client.getPage({ siteId, path, preview })
  ]);
  if ("entry" in pageResponse) {
    throw new Error(
      "This path resolves to a content entry, not a page. Use loadContent() instead, which handles both pages and entries. For entries, loadContent() returns the raw entry data for custom rendering."
    );
  }
  const { page: pageData } = pageResponse;
  const blocks = pageData.blocks.map((block) => {
    if (!block || typeof block !== "object") {
      throw new Error("Invalid block format in API response");
    }
    if (typeof block.id !== "string" && block.id !== null) {
      throw new Error(`Invalid block id: expected string or null, got ${typeof block.id}`);
    }
    if (typeof block.kind !== "string") {
      throw new Error(`Invalid block kind: expected string, got ${typeof block.kind}`);
    }
    if (typeof block.purpose !== "string") {
      throw new Error(`Invalid block purpose: expected string, got ${typeof block.purpose}`);
    }
    const typedBlock = block;
    return {
      id: typedBlock.id,
      kind: typedBlock.kind,
      purpose: typedBlock.purpose,
      // Include content for PageRenderer's getRenderableContent()
      content: _nullishCoalesce(typedBlock.content, () => ( {})),
      // Include draftContent if available (for preview mode)
      draftContent: _nullishCoalesce(_optionalChain([typedBlock, 'access', _ => _.draftContent, 'optionalAccess', _2 => _2.data]), () => ( null))
    };
  });
  const pageOutline = {
    name: pageData.name,
    path: pageData.path,
    purpose: pageData.purpose,
    blocks
  };
  const prefetchContext = {
    siteId,
    pageId: _nullishCoalesce(pageId, () => ( pageData.id)),
    previewStage: preview ? "preview" : "published"
  };
  const configData = await _chunkHOY77YBFjs.prefetchBlockData.call(void 0, 
    pageOutline,
    prefetchContext,
    client,
    {
      // Pass custom blocks so their config-based loaders are discovered
      customBlocks: _optionalChain([site, 'access', _3 => _3.sdkConfig, 'optionalAccess', _4 => _4.customBlocks])
    }
  );
  let resolvedData = configData;
  if (dataLoaderOverrides && Object.keys(dataLoaderOverrides).length > 0) {
    const codeData = await _chunkG4CKM4ENjs.executeCodeLoaders.call(void 0, pageOutline, prefetchContext, dataLoaderOverrides);
    resolvedData = _chunkG4CKM4ENjs.mergeLoaderResults.call(void 0, configData, codeData);
  }
  return {
    page: pageOutline,
    theme: site.theme,
    sdkConfig: _nullishCoalesce(site.sdkConfig, () => ( null)),
    siteId,
    resolvedData
    // Note: routeMap is optional and can be built from site data if needed for internal links.
    // Consumers can construct it from site.pages and pass explicitly via Page component props.
    // Example: const routeMap = site.pages.reduce((map, p) => ({ ...map, [p.id]: p.path }), {});
  };
}



exports.loadPage = loadPage;
//# sourceMappingURL=chunk-FPYK6527.js.map