/**
 * @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 { globSync } = require( 'glob' );

const utils = {
	/**
	 * Checks whether snippets should be built for specified `projectName`.
	 *
	 * @param {String} projectName Current parsed project name.
	 * @param {Object} hexo Options come from Hexo.
	 * @returns {Boolean}
	 */
	shouldBuildSnippets( projectName, hexo ) {
		if ( !hexo.projectGlobals || !hexo.projectGlobals[ projectName ] ) {
			return false;
		}

		if ( hexo.projectGlobals.common && hexo.projectGlobals.common.skipLiveSnippets ) {
			return false;
		}

		return true;
	},

	/**
	 * Returns all files that are required for building the snippet.
	 *
	 * @param {String} basePath An absolute path where the snippets are saved.
	 * @param {String} snippetName A name of the snippet which files are being looked for.
	 * @returns {SnippetSource}
	 */
	getSnippetSourcePaths( basePath, snippetName ) {
		const files = {};

		globSync( upath.join( basePath, snippetName + '.*' ) )
			.map( path => upath.normalize( path ) )
			.forEach( singlePath => {
				const fileExtension = upath.extname( singlePath ).slice( 1 );

				files[ fileExtension ] = singlePath;
			} );

		return files;
	},

	/**
	 * Returns a HTML Comment Node that will be replaced with the proper snippet later.
	 *
	 * @param {String} snippetName
	 * @returns {String}
	 */
	getSnippetPlaceholder( snippetName ) {
		return `<!--UMBERTO: SNIPPET: "${ snippetName }"-->`;
	}
};

module.exports = utils;
