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

const upath = require( 'upath' );
const getSdkSources = require( '../sdk-builder/get-sdk-sources' );
const TemplateCollection = require( '../template/template-collection' );
const SdkBuilder = require( '../sdk-builder/sdk-builder' );

module.exports = config => {
	const {
		outputDir, // build/docs
		themePath, // Umberto theme path
		BASE_PATH, // e.g.: ckeditor5/latest
		docSearch, // Algolia's docsearch config
		templateLocals, // Various data required by templates
		projectConfig, // umberto.json config
		canonicalUrlBeginning,
		projectRootPath,
		projectPath,
		srcPath,
		macrosVariables
	} = config;

	const theme = projectConfig.slug === 'ckeditor5' ? 'gloria' : 'umberto';

	const templateCollection = new TemplateCollection( upath.join( themePath, 'layout', theme, '_sdk' ), {
		locals: templateLocals
	} );
	const sdkBuilder = new SdkBuilder( {
		BASE_PATH,
		startPage: '/index.html',
		sdkCollection: getSdkSources( upath.join( projectRootPath, projectPath, srcPath ) ),
		template: templateCollection,
		config: projectConfig,
		destinationPath: upath.join( outputDir, BASE_PATH ),
		canonicalUrlBeginning,
		macrosVariables,
		docSearch
	} );

	sdkBuilder.buildSdk();

	return sdkBuilder;
};
