import { Metadata } from "next"

import { listCartOptions, retrieveCart } from "@lib/data/cart"
import { retrieveCustomer } from "@lib/data/customer"
import { getBaseURL } from "@lib/util/env"
import { StoreCartShippingOption } from "@medusajs/types"
import { DynamicLayoutRenderer } from "../../../vibentec/renderer"
import { LayoutContext, LayoutComponentNode,  } from "../../../vibentec/component-map"
import { loadDesignConfig } from "vibentec/configloader"

export const metadata: Metadata = {
  metadataBase: new URL(getBaseURL()),
}

export default async function PageLayout(props: { children: React.ReactNode }) {
  const customer = await retrieveCustomer()
  const cart = await retrieveCart()
  let shippingOptions: StoreCartShippingOption[] = []

  if (cart) {
    const { shipping_options } = await listCartOptions()

    shippingOptions = shipping_options
  }

  const nodes: LayoutComponentNode[] = await loadDesignConfig()
  const context: LayoutContext = {
    customer,
    cart,
    shippingOptions,
    contentChildren: props.children,
  }


  return <DynamicLayoutRenderer nodes={nodes} context={context} />
}
