const path = require('path')
const fs = require('fs')
const configStore = require('./../config.js')

/**
 *  asset - checks manifest.json for given path and return
 *  file path with id for cache busting
 *
 *
 *  @param {String} publicPath
 *
 */

function asset(staticPath) {

    // getting basePath
    let result = staticPath

    // path to mix-manifest
    const file = path.join(configStore.get('destination')) + '/mix-manifest.json'

    if (fs.existsSync(file)) {

        const manifest = fs.readFileSync(file)
        const files = JSON.parse(manifest)

        if (files[staticPath]) {
            result = files[staticPath]
        }
    }

    return result
}

module.exports = { asset }
