/**
 * Session memory for style-scope choices across inspector remounts.
 * Page reload clears this Map.
 * - "instance" is also written to block attributes so it survives reload.
 * Linked placements leave scope unset and keep following My Block Design.
 *
 * Scope is always block-level (`__block__`) — never per element.
 * Memory keys are per placement (surfaceId), so one block does not affect another.
 */

export const BLOCK_STYLE_SCOPE_KEY = '__block__';

const choices = new Map();

/**
 * @param {string|number} surfaceId Block placement id (Gutenberg clientId, FE node id, …)
 * @param {string} [targetKey] Ignored — kept for call-site compat; always block scope
 * @return {string}
 */
export function styleScopeMemoryKey(
	surfaceId,
	targetKey = BLOCK_STYLE_SCOPE_KEY
) {
	return `${ surfaceId || 'anon' }::${ targetKey || BLOCK_STYLE_SCOPE_KEY }`;
}

/**
 * @param {string|number} surfaceId
 * @param {string} [targetKey]
 * @return {'instance'|null}
 */
export function getRememberedStyleScope(
	surfaceId,
	targetKey = BLOCK_STYLE_SCOPE_KEY
) {
	const value = choices.get( styleScopeMemoryKey( surfaceId, targetKey ) );
	return value === 'instance' ? 'instance' : null;
}

/**
 * @param {string|number} surfaceId
 * @param {string} [targetKey]
 * @param {'instance'|null} choice
 */
export function rememberStyleScope(
	surfaceId,
	targetKey = BLOCK_STYLE_SCOPE_KEY,
	choice
) {
	const key = styleScopeMemoryKey( surfaceId, targetKey );
	if ( choice !== 'instance' ) {
		choices.delete( key );
		return;
	}
	choices.set( key, 'instance' );
}
