/**
 * @license Copyright (c) 2017-2025, CKSource Holding sp. z o.o. All rights reserved.
 * For licensing, see LICENSE.md.
 */

'use strict';

const upath = require( 'upath' );
const fs = require( 'fs' );

/**
 * Checks if the project has its own favicons. If so, they will be used when rendering meta tags (`<link rel="icon">`).
 *
 * Otherwise, the default favicons will be used.
 *
 * @param {object} page
 * @param {string} page.projectName
 * @param {boolean} page.hasOwnAssets
 * @param {object} hexo
 * @param {Object.<string, ProjectGlobalConfig>} hexo.projectGlobals
 * @returns {*}
 */
module.exports = function hasOwnFavicons( page, hexo ) {
	const projectConfig = hexo.projectGlobals[ page.projectName ];

	let hasOwnAssets = false;

	if ( projectConfig ) {
		const { config } = hexo.projectGlobals[ page.projectName ];

		if ( config ) {
			hasOwnAssets = fs.existsSync(
				upath.join( config.projectRootPath, 'docs', 'assets', 'img', 'favicons', '96x96.png' )
			);
		}
	}

	page.hasOwnAssets = hasOwnAssets;

	return page;
};

/**
 * @typedef {object} ProjectGlobalConfig
 * @property {string} projectRootPath
 * @property {object} config
 * @property {string} config.projectRootPath
 */
