"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.default = nextFontLoader;
var _path = _interopRequireDefault(require("path"));
var _loaderUtils3 = _interopRequireDefault(require("next/dist/compiled/loader-utils3"));
var _postcssFontLoader = _interopRequireDefault(require("./postcss-font-loader"));
var _util = require("util");
var _chalk = _interopRequireDefault(require("next/dist/compiled/chalk"));
async function nextFontLoader() {
    const fontLoaderSpan = this.currentTraceSpan.traceChild("next-font-loader");
    return fontLoaderSpan.traceAsyncFn(async ()=>{
        const callback = this.async();
        const { isServer , assetPrefix , fontLoaderOptions , postcss: getPostcss ,  } = this.getOptions();
        const emitFontFile = (content, ext, preload)=>{
            const opts = {
                context: this.rootContext,
                content
            };
            const interpolatedName = _loaderUtils3.default.interpolateName(this, // Font files ending with .p.(woff|woff2|eot|ttf|otf) are preloaded
            `static/media/[hash]${preload ? ".p" : ""}.${ext}`, opts);
            const outputPath = `${assetPrefix}/_next/${interpolatedName}`;
            if (!isServer) {
                this.emitFile(interpolatedName, content, null);
            }
            return outputPath;
        };
        // next-swc next_font_loaders turns each function call argument into JSON seperated by semicolons
        let [relativeFilePathFromRoot, functionName, ...data] = this.resourceQuery.slice(1).split(";");
        data = data.map((value)=>JSON.parse(value));
        try {
            const fontLoader = require(_path.default.join(this.resourcePath, "../loader.js")).default;
            let { css , fallbackFonts , adjustFontFallback , weight , style , variable  } = await fontLoader({
                functionName,
                data,
                config: fontLoaderOptions,
                emitFontFile,
                resolve: (src)=>(0, _util).promisify(this.resolve)(_path.default.dirname(_path.default.join(this.rootContext, relativeFilePathFromRoot)), src),
                fs: this.fs
            });
            const { postcss  } = await getPostcss();
            // Exports will be exported as is from css-loader instead of a CSS module export
            const exports = [];
            const fontFamilyHash = _loaderUtils3.default.getHashDigest(Buffer.from(css), "md5", "hex", 6);
            // Add CSS classes, exports and make the font-family localy scoped by turning it unguessable
            const result = await postcss((0, _postcssFontLoader).default({
                exports,
                fontFamilyHash,
                fallbackFonts,
                weight,
                style,
                adjustFontFallback,
                variable
            })).process(css, {
                from: undefined
            });
            // Reuse ast in css-loader
            const ast = {
                type: "postcss",
                version: result.processor.version,
                root: result.root
            };
            callback(null, result.css, null, {
                exports,
                ast,
                fontFamilyHash
            });
        } catch (err) {
            err.stack = false;
            err.message = `Font loader error:\n${err.message}`;
            err.message += `

${_chalk.default.cyan(`Location: ${relativeFilePathFromRoot}`)}`;
            callback(err);
        }
    });
}
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

//# sourceMappingURL=index.js.map