import { createPage, createSimpleGetStaticProps } from './createPage';
import type { MasterPageConfig } from '@2fn/conf';
import type { PageGeneratorProps } from './generatePages';

export default async function createMasterPage({
  pagesDir,
  name,
  config,
}: PageGeneratorProps<MasterPageConfig>) {
  console.log(`creating master page ${name} in ${pagesDir}`);
  const fileName = await createPage(
    pagesDir,
    name,
    getMasterPageContent(
      config.properties,
      config.detail.page,
      config.detail.key
    )
  );
  createSimpleGetStaticProps(fileName, config.category);
}

function getMasterPageContent(properties: string[], page: string, key: string) {
  return `<div>
  {data.map(item => <Link href={\`/${page}/\${item['${[key]}']}\`} key={item['${
    properties[0]
  }']}><a>${properties.map(
    (prop) => `<span>{item['${prop}']}</span>`
  )}</a></Link>
  )}
</div>`;
}
