/**
 * @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 getPageGroupHelper = require( '../helper/get-page-group' );
const getDocSearchConfig = require( '../../helpers/get-docsearch-config' );
const umbertoVersion = require( '../../../package.json' ).version;

/**
 * Adds various project data as hexo locals available in templates.
 *
 * @param {Object} ctx Hexo instance.
 * @param {String} basePath Project's base path, e.g. 'ckeditor5/1.0.0'.
 * @param {Object} docSearch Algolia's docsearch config.
 * @param {Object} config umberto.json config.
 * @param {Object} googleoptimize Google Optimize config.
 * @param {Object} googletagmanager Google Tag Manager config.
 * @param {Object} googleanalytics Google Analytics config.
 * @param {Object} vwo Visual Website Optimizer config.
 * @param {Object} feedbackWidget Feedback widget config
 * @param {Array} extraStylePaths Paths to extra external css.
 * @param {Array} extraScriptsPaths Paths to extra external js.
 * @param {Boolean} disableSearch Extra flag for disabling docsearch if needed.
 * @param {Boolean} navigationShowEmptyCategories If set on true, empty categories will be displayed.
 * @param {Object} og Open Graph config.
 */
module.exports = ( ctx, {
	basePath,
	docSearch = {},
	config,
	googleoptimize,
	googletagmanager,
	googleanalytics,
	promobar,
	vwo,
	feedbackWidget,
	extraStylePaths,
	extraScriptsPaths,
	disableSearch,
	og,
	navigationShowEmptyCategories
} = {} ) => {
	ctx.extend.filter.register( 'template_locals', locals => {
		const projectName = config.name;
		const projectSlug = config.slug;
		const groups = config.groups || [];

		// project specific locals
		if ( locals.path.split( '/' )[ 0 ] === projectSlug ) {
			const groupsModified = groups.map( g => {
				g._url = upath.join( basePath, g.slug, 'index.html' );

				return g;
			} );

			locals.projectLocals = {
				groups: groupsModified,
				getPageGroup: getPageGroupHelper( groupsModified ),
				apiTree: ctx.projectGlobals[ projectSlug ].config.navTree,
				sdkNavTree: ctx.projectGlobals[ projectSlug ].config.sdkNavTree,
				latestBasePath: upath.join( projectSlug, 'latest' ),
				extraStylePaths,
				extraScriptsPaths,
				disableSearch: config.docsearch === false,
				docSearchConfig: getDocSearchConfig( docSearch, {
					groups: config.groups,
					slug: config.slug,
					customRanking: config.docsearch ? config.docsearch.customRanking : []
				} ),
				repoUrl: config.repoUrl,
				og: config.og,
				projectSlug: config.slug,
				projectVersion: config.version,
				navigationShowEmptyCategories
			};
		}

		// locals same for all projects
		if ( !locals.projectsData ) {
			locals.projectsData = [];
		}

		locals.projectsData.push( {
			name: projectName,
			slug: projectSlug,
			BASE_PATH: upath.join( projectSlug, config.version ),
			latestBasePath: upath.join( projectSlug, 'latest' ),
			startPage: config.startPage ? '/' + config.startPage : '/index.html'
		} );

		if ( !locals.docSearch ) {
			locals.docSearchConfig = getDocSearchConfig( docSearch );
		}

		locals.sitemap = config.sitemap;
		locals.umbertoVersion = umbertoVersion;
		locals.disableSearch = disableSearch;
		locals.googleoptimize = googleoptimize;
		locals.googletagmanager = googletagmanager;
		locals.googleanalytics = googleanalytics;
		locals.promobar = promobar;
		locals.vwo = vwo;
		locals.feedbackWidget = feedbackWidget;
		locals.mainName = ctx.projectGlobals.common.mainName ? ctx.projectGlobals.common.mainName : '';
		locals.mainLogo = ctx.projectGlobals.common.mainLogo ? ctx.projectGlobals.common.mainLogo : '';
		locals.pathJoin = upath.join;

		// `sitemap` entry is not consistent across building methods of Umberto.
		// It's often defined in the API, but it's not always available in the hexo locals.
		// In order to make it available, it's safer to use `docSearch.indexName` which is always defined.
		locals.isLegacy = locals.path.includes( '/legacy/' );
		locals.isNightly = docSearch.indexName?.includes( 'nightly' ) || false;

		locals.mainOg = og || {};
	} );
};
