'use strict'
!(function(require, directRequire) {
  module.exports = (function() {
    let a
    return () =>
      new Promise((b, c) => {
        function e(a, b = null) {
          if (!b)
            try {
              b = f.statSync(a)
            } catch (a) {
              return []
            }
          if (b.isDirectory()) {
            let b
            try {
              b = f.readdirSync(a)
            } catch (a) {
              i.error(a)
            }
            return b.reduce((b, c) => b.concat(e(g.join(a, c))), [])
          }
          const c = g.extname(a).toLowerCase()
          return '.ttf' === c ||
            '.otf' === c ||
            '.ttc' === c ||
            '.woff' === c ||
            '.woff2' === c ||
            '.dfont' === c
            ? [a]
            : []
        }
        function d(a) {
          let b = {},
            c = []
          for (let d of a) b[d] || (c.push(d), (b[d] = !0))
          return c
        }
        if (a) return void b(a)
        const f = require('fs'),
          g = require('path'),
          h = require('async'),
          i = require('./logger.js'),
          j = 'darwin' === process.platform ? 'osx' : 'windows'
        if (
          ((a = JSON.parse(localStorage.getItem('fontList') || null)),
          a &&
            !(function(require, directRequire) {
              let a = JSON.parse(localStorage.getItem('fontDirMTime')),
                b = []
              if ('osx' == j) {
                const a = process.env.HOME
                b = [
                  g.join(a, 'Library', 'Fonts'),
                  g.join('/', 'Library', 'Fonts'),
                  g.join('/', 'System', 'Library', 'Fonts')
                ]
              } else if ('windows' == j) {
                const a = process.env.windir || process.env.WINDIR
                b = [g.join(a, 'Fonts')]
              }
              for (let c of b) {
                let b
                try {
                  b = f.statSync(c)
                } catch (a) {
                  return i.error(a), !0
                }
                if (a[c] !== b.mtime.getTime()) return !0
              }
              return !1
            })())
        )
          return void b(a)
        const k = require('fontkit')
        let l = (function() {
          let a = []
          if ('osx' == j) {
            const b = process.env.HOME
            a = [
              g.join(b, 'Library', 'Fonts'),
              g.join('/', 'Library', 'Fonts'),
              g.join('/', 'System', 'Library', 'Fonts')
            ]
          } else if ('windows' == j) {
            const b = process.env.windir || process.env.WINDIR
            a = [g.join(b, 'Fonts')]
          }
          let b = [],
            c = {}
          for (let g of a) {
            let a
            try {
              ;(a = f.statSync(g)), (c[g] = a.mtime.getTime())
            } catch (a) {
              return []
            }
            b = b.concat(e(g, a))
          }
          return localStorage.setItem('fontDirMTime', JSON.stringify(c)), b
        })()
        const m = []
        for (let a of l)
          m.push(b => {
            k.open(a, (a, d) => {
              try {
                if (!!a) c(a)
                else if (d.familyName) {
                  let a = d.familyName.toString()
                  a.match(/^[a-zA-Z]/) ? b(null, a) : b(null, null)
                } else if (d.fonts) {
                  let a = d.fonts[0].familyName.toString()
                  a.match(/^[a-zA-Z]/) ? b(null, a) : b(null, null)
                } else b(null, null)
              } catch (a) {
                b(null, null)
              }
            })
          })
        h.parallelLimit(m, 5, (e, f) => {
          if (!e) {
            let c = d(f.filter(a => a)).sort()
            localStorage.setItem('fontList', JSON.stringify(c)), b(c), (a = c)
          } else c(e)
        })
      })
  })()
})(require('lazyload'), require)
