import i18n from 'i18n-calypso';
import { get } from 'lodash';
import { withoutHttp } from 'calypso/lib/url';

export function userCan( capability, site ) {
	return site && site.capabilities && site.capabilities[ capability ];
}

/**
 * site's timezone getter
 * @param   {Object} site - site object
 * @returns {string} timezone
 */
export function timezone( site ) {
	return site && site.options ? site.options.timezone : null;
}

/**
 * site's gmt_offset getter
 * @param   {Object} site - site object
 * @returns {string} gmt_offset
 */
export function gmtOffset( site ) {
	return site && site.options ? site.options.gmt_offset : null;
}

export function getSiteFileModDisableReason( site, action = 'modifyFiles' ) {
	if ( ! site || ! site.options || ! site.options.file_mod_disabled ) {
		return;
	}

	return site.options.file_mod_disabled
		.map( ( clue ) => {
			if (
				action === 'modifyFiles' ||
				action === 'autoupdateFiles' ||
				action === 'autoupdateCore'
			) {
				if ( clue === 'has_no_file_system_write_access' ) {
					return i18n.translate( 'The file permissions on this host prevent editing files.' );
				}
				if ( clue === 'disallow_file_mods' ) {
					return i18n.translate(
						'File modifications are explicitly disabled by a site administrator.'
					);
				}
			}

			if (
				( action === 'autoupdateFiles' || action === 'autoupdateCore' ) &&
				clue === 'automatic_updater_disabled'
			) {
				return i18n.translate( 'Any autoupdates are explicitly disabled by a site administrator.' );
			}

			if ( action === 'autoupdateCore' && clue === 'wp_auto_update_core_disabled' ) {
				return i18n.translate(
					'Core autoupdates are explicitly disabled by a site administrator.'
				);
			}
			return null;
		} )
		.filter( ( reason ) => reason );
}

export function isMainNetworkSite( site ) {
	if ( ! site ) {
		return false;
	}

	if ( site.options && site.options.is_multi_network ) {
		return false;
	}

	if ( site.is_multisite === false ) {
		return true;
	}

	if ( site.is_multisite ) {
		const unmappedUrl = get( site.options, 'unmapped_url', null );
		const mainNetworkSite = get( site.options, 'main_network_site', null );
		if ( ! unmappedUrl || ! mainNetworkSite ) {
			return false;
		}
		// Compare unmapped_url with the main_network_site to see if is the main network site.
		return ! ( withoutHttp( unmappedUrl ) !== withoutHttp( mainNetworkSite ) );
	}

	return false;
}

/**
 * Checks whether a site has a custom mapped URL.
 * @param   {undefined|null|{domain?: string; wpcom_url?: string}}   site Site object
 * @returns {boolean|null}      Whether site has custom domain
 */
export function hasCustomDomain( site ) {
	if ( ! site || ! site.domain || ! site.wpcom_url ) {
		return null;
	}

	return site.domain !== site.wpcom_url;
}

export function isModuleActive( site, moduleId ) {
	return site.options?.active_modules?.includes( moduleId );
}

/**
 * Returns the WordPress.com URL of a site (simple or Atomic)
 * @param {Object} site Site object
 * @returns {?string} WordPress.com URL
 */
export function getUnmappedUrl( site ) {
	if ( ! site || ! site.options ) {
		return null;
	}

	return site.options.main_network_site || site.options.unmapped_url;
}

/**
 * Returns a filtered array of WordPress.com site IDs where a Jetpack site
 * exists in the set of sites with the same URL.
 * @param {Array} siteList Array of site objects
 * @returns {number[]} Array of site IDs with URL collisions
 */
export function getJetpackSiteCollisions( siteList ) {
	return siteList
		.filter( ( siteItem ) => {
			const siteUrlSansProtocol = withoutHttp( siteItem.URL );
			return (
				! siteItem.jetpack &&
				siteList.some(
					( jetpackSite ) =>
						jetpackSite.jetpack && siteUrlSansProtocol === withoutHttp( jetpackSite.URL )
				)
			);
		} )
		.map( ( siteItem ) => siteItem.ID );
}

const P2_THEMES = [ 'pub/p2', 'pub/p2-breathe', 'pub/p2-hub', 'pub/p2020' ];

/**
 * Returns whether the theme is a P2 theme
 * @param {string} themeSlug The slug of the theme to check
 * @returns {boolean} Whether the theme is a P2 theme
 */
export function isP2Theme( themeSlug ) {
	return P2_THEMES.includes( themeSlug );
}
