text: 'use strict';
  
  const Promise = require('bluebird');
  const _ = require('lodash');
  const path = require('path');
  const mz = require('mz');
  const fs = require('fs');
  const ttf2eot = require('ttf2eot');
  const ttf2woff = require('ttf2woff');
  const wawoff2 = require('wawoff2');
  const svg2ttf = require('svg2ttf');
  const pug = require('pug');
  const b64 = require('base64-js');
  
  
  
  //const UID = (new Date()).getTime().toString(36); //CURRENT TIME - BASE 36 ENCODED
  const TEMPLATES_DIR = path.join(__dirname, 'templates');
  const TEMPLATES = {};
  const SVG_FONT_TEMPLATE = _.template(
    fs.readFileSync(path.join(TEMPLATES_DIR, 'svg.tpl'), 'utf8')
  );
  
  
  function compileTemplates(){
    _.forEach(
      {
        'demo.pug': 'font-icons-preview.html',
        'list.pug': 'font-icons-list.csv',
        'css-base.pug': '${PATH}${FONTNAME}-base.css',
        'amp-css-base.pug': '${PATH}${FONTNAME}-amp-base.css',
        'css-codes.pug': '${PATH}${FONTNAME}-codes.css'
      },
      (outputName, inputName) => {
        const inputFile = path.join(TEMPLATES_DIR, inputName);
        const inputData = fs.readFileSync(inputFile, 'utf8');
        let outputData;
  
        switch (path.extname(inputName)) {
          case '.pug':
            outputData = pug.compile(inputData, {
              pretty: true,
              filename: inputFile,
              filters: [require('jstransformer-stylus')]
            });
            break;
  
          case '.tpl':
            outputData = _.template(inputData);
            break;
  
          default:
            outputData = () => inputData;
            break;
        }
  
        TEMPLATES[outputName] = outputData;
      }
    );
  }
  
  module.exports = async function fontWorker(taskInfo) {
  
    compileTemplates();
  
    const fontname = taskInfo.builderConfig.font.fontname;
  
    const files = {
      //config: path.join(taskInfo.tmpDir, 'config.json'),
      svg: path.join(taskInfo.fontDir, `${ taskInfo.fontId }.svg`),
      ttf: path.join(taskInfo.fontDir, `${ taskInfo.fontId }.ttf`),
      ttfUnhinted: path.join(taskInfo.fontDir, `${ taskInfo.fontId }-unhinted.ttf`),
      eot: path.join(taskInfo.fontDir, `${ taskInfo.fontId }.eot`),
      woff: path.join(taskInfo.fontDir, `${ taskInfo.fontId }.woff`),
      woff2: path.join(taskInfo.fontDir, `${ taskInfo.fontId }.woff2`)
    };
  
    const svgOutput = SVG_FONT_TEMPLATE(taskInfo.builderConfig);
  
    // Prepare temporary working directory.
  
    //await fs.promises.rmdir(taskInfo.tmpDir, { recursive: true });
    fs.rmdirSync(taskInfo.tmpDir, { recursive: true });
  
    await fs.promises.mkdir(taskInfo.tmpDir, { recursive: true });
    await fs.promises.mkdir(taskInfo.fontDir, { recursive: true });
    await fs.promises.mkdir(taskInfo.cssDir, { recursive: true });
  
    // const configOutput = JSON.stringify(taskInfo.clientConfig, null, '  ');
    // await mz.fs.writeFile(files.config, configOutput, 'utf8');
    await mz.fs.writeFile(files.svg, svgOutput, 'utf8');
  
    const ttf = svg2ttf(svgOutput, {
      copyright: taskInfo.builderConfig.font.copyright
    });
    await mz.fs.writeFile(files.ttf, ttf.buffer);
  
    const ttfOutput = new Uint8Array(await mz.fs.readFile(files.ttf));
    const eotOutput = ttf2eot(ttfOutput).buffer;
    await mz.fs.writeFile(files.eot, eotOutput);
  
    const woffOutput = ttf2woff(ttfOutput).buffer;
    await mz.fs.writeFile(files.woff, woffOutput);
  
    const woff2Output = await wawoff2.compress(ttfOutput);
    await mz.fs.writeFile(files.woff2, woff2Output);
  
    const templatesNames = Object.keys(TEMPLATES);
    for (let i = 0; i < templatesNames.length; i++) {
      const templateName = templatesNames[i];
      const templateData = TEMPLATES[templateName];
  
      if (
        templateName === 'LICENSE.txt' &&
        !taskInfo.builderConfig.fonts_list.length
      ) {
        continue;
      }
  
      const outputName = templateName
        .replace('${FONTNAME}', fontname)
        .replace('${PATH}', taskInfo.cssDirName ? taskInfo.cssDirName +'/' : '');
  
      const outputFile = path.join(taskInfo.tmpDir, outputName);
      const outputData = templateData(taskInfo.builderConfig)
        .replace('%WOFF64%', b64.fromByteArray(woffOutput))
        .replace('%TTF64%', b64.fromByteArray(ttfOutput));
  
      await mz.fs.writeFile(outputFile, outputData, 'utf8');
    }
  };
  