"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
exports.discoverStaticMetadataFiles = discoverStaticMetadataFiles;
exports.buildMetadata = buildMetadata;
exports.METADATA_IMAGE_RESOURCE_QUERY = void 0;
var _path = _interopRequireDefault(require("path"));
var _querystring = require("querystring");
function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const METADATA_TYPE = "metadata";
const METADATA_IMAGE_RESOURCE_QUERY = "?__next_metadata";
exports.METADATA_IMAGE_RESOURCE_QUERY = METADATA_IMAGE_RESOURCE_QUERY;
const staticAssetIconsImage = {
    icon: {
        filename: "icon",
        extensions: [
            "ico",
            "jpg",
            "jpeg",
            "png",
            "svg"
        ]
    },
    apple: {
        filename: "apple-icon",
        extensions: [
            "jpg",
            "jpeg",
            "png",
            "svg"
        ]
    }
};
// Produce all compositions with filename (icon, apple-icon, etc.) with extensions (png, jpg, etc.)
async function enumMetadataFiles(dir, filename, extensions, { resolvePath , addDependency , addMissingDependency  }) {
    const collectedFiles = [];
    // Possible filename without extension could: icon, icon0, ..., icon9
    const possibleFileNames = [
        filename
    ].concat(Array(10).fill(0).map((_, index)=>filename + index));
    for (const name of possibleFileNames){
        for (const ext of extensions){
            const pathname = _path.default.join(dir, `${name}.${ext}`);
            try {
                const resolved = await resolvePath(pathname);
                addDependency(resolved);
                collectedFiles.push(resolved);
            } catch (err) {
                if (!err.message.includes("Can't resolve")) {
                    throw err;
                }
                addMissingDependency(pathname);
            }
        }
    }
    return collectedFiles;
}
async function discoverStaticMetadataFiles(resolvedDir, { isDev , resolvePath , addDependency , addMissingDependency  }) {
    let hasStaticMetadataFiles = false;
    const iconsMetadata = {
        icon: [],
        apple: []
    };
    const opts = {
        resolvePath,
        addDependency,
        addMissingDependency
    };
    async function collectIconModuleIfExists(type) {
        const resolvedMetadataFiles = await enumMetadataFiles(resolvedDir, staticAssetIconsImage[type].filename, staticAssetIconsImage[type].extensions, opts);
        resolvedMetadataFiles.sort((a, b)=>a.localeCompare(b)).forEach((filepath)=>{
            const iconModule = `() => import(/* webpackMode: "eager" */ ${JSON.stringify(`next-metadata-image-loader?${(0, _querystring).stringify({
                isDev
            })}!` + filepath + METADATA_IMAGE_RESOURCE_QUERY)})`;
            hasStaticMetadataFiles = true;
            iconsMetadata[type].push(iconModule);
        });
    }
    await Promise.all([
        collectIconModuleIfExists("icon"),
        collectIconModuleIfExists("apple"), 
    ]);
    return hasStaticMetadataFiles ? iconsMetadata : null;
}
function buildMetadata(metadata) {
    return metadata ? `${METADATA_TYPE}: {
    icon: [${metadata.icon.join(",")}],
    apple: [${metadata.apple.join(",")}]
  }` : "";
}

//# sourceMappingURL=metadata.js.map