/* eslint-disable no-console */

const fs = require('fs/promises');
const CreativeEngine = require('./index.js');

const license = process.env.CESDK_LICENSE;

CreativeEngine.init({ license }).then(async (engine) => {
  await engine.addDefaultAssetSources();
  console.log('Loading scene');
  engine.scene
    .loadFromURL(`file://${__dirname}/demo.scene`)
    .then(() => {
      console.log('Scene loaded, exporting page');
      const [page] = engine.block.findByType('page');
      return engine.block.export(page, { mimeType: 'image/png' });
    })
    .then((blob) => blob.arrayBuffer())
    .then((arrayBuffer) => {
      console.log('Writing output: example-output.png');
      return fs.writeFile('./example-output.png', Buffer.from(arrayBuffer));
    })
    .catch((err) => {
      console.error(err);
    })
    .finally(() => {
      engine.dispose();
    });
});
