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

'use strict';

const { minimatch } = require( 'minimatch' );

/**
 * @param {Object} page A page object.
 * @param {String} page.path A relative path where a page will be stored.
 * @param {Object} config The project configuration.
 * @param {Object} config.reportIssueWidget The report issue widget configuration.
 * @param {Boolean} config.reportIssueWidget.enabled Whether the report issue widget is enabled.
 * @param {Array.<String>} config.reportIssueWidget.skipPages An array of glob patterns that define pages where
 * the report issue widget should be disabled.
 * @param {String} config.reportIssueWidget.issueUrl The URL that leads to a form where a user can create a new issue.
 * @return {String|null}
 */
module.exports = function getReportIssueWidgetUrl( page, config ) {
	if ( !config.reportIssueWidget.enabled ) {
		return null;
	}

	const isAllowedOnPage = !config.reportIssueWidget.skipPages.some( pattern => {
		// Map a pattern to: `<slug>/<version>/<path>`.
		const globPatern = config.slug + '/*/' + pattern;

		return minimatch( page.path, globPatern );
	} );

	if ( !isAllowedOnPage ) {
		return null;
	}

	return config.reportIssueWidget.issueUrl;
};
