import Head from 'next/head';
import { readFile, utils } from 'xlsx';
import { join } from 'path';
import { cwd } from 'process';
export default function Index({html, type, name}) { return (
  
    
    SheetJS Next.JS {type} Demo
    
    
  
  
SheetJS Next.JS {type} Demo
This demo reads from /public/sheetjs.xlsx.
{name}
  
  
 
); }
let cache = [];
export async function getStaticProps(ctx) {
  if(!cache || !cache.length) {
    const wb = readFile(join(cwd(), "public", "sheetjs.xlsx"));
    cache = wb.SheetNames.map((name) => ({ name, sheet: wb.Sheets[name] }));
  }
  const entry = cache[ctx.params.id];
  return {
    props: {
      type: "getStaticPaths",
      name: entry.name,
      id: ctx.params.id.toString(),
      html: entry.sheet ? utils.sheet_to_html(entry.sheet) : "",
    },
  }
}
export async function getStaticPaths() {
  const wb = readFile(join(cwd(), "public", "sheetjs.xlsx"));
  cache = wb.SheetNames.map((name) => ({ name, sheet: wb.Sheets[name] }));
  return {
    paths: wb.SheetNames.map((name, idx) => ({ params: { id: idx.toString()  } })),
    fallback: false,
  };
}