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

'use strict';

const HtmlFile = require( '../api-builder/classes/html-file' );
const writeHtmlFiles = require( './write-html-files' );

/**
 * Redirection page for urls like https://docs.ckeditor.com/ckeditor5/.
 * In this case it redirects to https://docs.ckeditor.com/ckeditor5/latest/.
 *
 * @param destination
 * @param title
 * @param url
 * @returns {Promise}
 */
module.exports = ( {
	destination,
	title,
	url
} = {} ) => {
	const content = [
		'<!DOCTYPE HTML>',
		'<html lang="en">',
		'<head>',
		'<meta charset="utf-8">',
		'<meta name="viewport" content="width=device-width, initial-scale=1">',
		`<meta http-equiv="refresh" content="1; url=${ url }">`,
		'<script type="text/javascript">',
		`window.location.href = "${ url }";`,
		'</script>',
		`<title>${ title }</title>`,
		'</head>',
		'<body>',
		`If you are not redirected automatically, follow this <a href="${ url }" data-skip-validation="true">link</a>.`,
		'</body>',
		'</html>'
	].join( '\n' );

	const page = new HtmlFile( content, destination );

	return writeHtmlFiles( [ page ] );
};
