import {
  createPage,
  createDynamicGetStaticProps,
  createGetStaticPaths,
} from './createPage';
import type { DynamicMasterPageConfig } from '@2fn/conf';
import type { PageGeneratorProps } from './generatePages';

export default async function createDynamicMasterPage({
  pagesDir,
  name,
  config,
}: PageGeneratorProps<DynamicMasterPageConfig>) {
  console.log(`creating dynamic master page ${name} in ${pagesDir}`);
  const fileName = await createPage(
    pagesDir,
    name,
    getDynamicMasterPageContent(
      config.properties,
      config.detail.page,
      config.detail.key
    )
  );
  createDynamicGetStaticProps(fileName, config.category);
  createGetStaticPaths(
    fileName,
    config.dynamicData.category,
    config.dynamicData.key
  );
}

function getDynamicMasterPageContent(
  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>`;
}
