// import { readFile } from 'fs/promises';
import archiver from 'archiver';
import {createWriteStream} from 'fs';
import {readdir} from 'fs/promises';
import assert from 'assert';

// const act = <T extends (...args: any[]) => any>(fn: T) => {
// 	return async (...args: Parameters<T>) => {
// 		let r;
// 		try {
// 			r = await fn(...args);
// 		} catch {
// 			// noop
// 		}
// 		return r;
// 	}
// };

const main = async () => {

	// await act(cp)('dist', 'book', {recursive: true});
	// await act(rm)('dist', {recursive: true, force: true});
	// await act(mkdir)('dist');
	// await act(cp)('book', 'dist/book', {recursive: true});

	const { default: astroConfig } = await import('../astro.config.mjs');
	const { outDir = 'dist/', output = 'static', build, compressHTML = false } = astroConfig;
	assert(output === 'static');
	assert(build?.format === 'file');
	assert(!compressHTML)
	const distFiles = await readdir(outDir);
	// TODO get all assets and declare to manifest
	console.log(distFiles);

	return new Promise<number>((resolve, reject) => {
		const output = createWriteStream('book.epub');
		const archive = archiver('zip', {
			zlib: {
				level: 9,
			},
		});

		output.on('close', () => {
			resolve(archive.pointer());
		});

		archive.on('error', (err) => {
			reject(err);
		});

		archive.pipe(output);

		archive.append('application/epub+zip', { name: 'mimetype' });
		archive.append(`<?xml version="1.0"?>
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
 <rootfiles>
  <rootfile full-path="content.opf" media-type="application/oebps-package+xml"/>
 </rootfiles>
</container>
`, { name: 'META-INF/container.xml' });
		archive.append(`<?xml version="1.0"?>
<package xmlns="http://www.idpf.org/2007/opf" xmlns:opf="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="BookID">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
   <dc:identifier id="BookID" opf:scheme="isbn">978-3-86680-192-9</dc:identifier>
   <dc:identifier>3b622266-b838-4003-bcb8-b126ee6ae1a2</dc:identifier>
   <dc:title>The title</dc:title>
   <dc:language>fr</dc:language>
   <dc:publisher>The publisher</dc:publisher>
   <dc:creator>The author</dc:creator>
   <dc:contributor>A contributor</dc:contributor>
   <dc:description>A description</dc:description>
   <dc:subject>A subject of the publication</dc:subject>
   <dc:subject>Another subject of the publication</dc:subject>
   <dc:rights>© copyright notice</dc:rights>
   <meta property="dcterms:modified">2020-01-01T01:01:01Z</meta>
  </metadata>
  <manifest>
   <item id="coverimage" href="OEBPS/cover.jpg" media-type="image/jpeg" properties="cover-image"/>
   <item id="cover" href="OEBPS/index.xhtml" media-type="application/xhtml+xml"/>
   <item id="toc" href="toc.html" media-type="application/xhtml+xml" properties="nav"/>
   
   <item id="chapter-1" href="Text/chapter-1.xhtml" media-type="application/xhtml+xml"/>
   <item id="chapter-2" href="Text/chapter-2.xhtml" media-type="application/xhtml+xml"/>
   
   <item id="css" href="Styles/publication.css" media-type="text/css"/>
   
   <item id="font1" href="Fonts/Andada-Italic.otf" media-type="application/vnd.ms-opentype"/>
   <item id="font2" href="Fonts/Andada-Regular.otf" media-type="application/vnd.ms-opentype"/>
   <item id="glyph" href="Images/glyph.png" media-type="image/png"/>
  </manifest>
  <spine>
   <itemref idref="cover"/>
   <itemref idref="toc"/>
   <itemref idref="chapter-1"/>
   <itemref idref="chapter-2"/>
  </spine>
</package>
`, { name: 'content.opf' });
		archive.directory(outDir, 'OEBPS');
	});
};

main().then((bytes) => {
	console.log(`${bytes} written.`);
});
