import {
  BROWSER_OPTIONS,
  PREVIEW_VARIANTS,
} from '../constants';

/**
 * Returns the browser options allowed for a given OS.
 * Safari is supported on macOS, iOS, and iPadOS.
 * All browsers are supported on macOS, iOS, and iPadOS: Chrome, Safari, Opera, Firefox, Edge.
 * Other OSes exclude Safari.
 */
export const getBrowserOptionsForOS = (os) => {
  if (os === 'macOS' || os === 'iOS' || os === 'iPadOS') {
    return BROWSER_OPTIONS;
  }
  return BROWSER_OPTIONS.filter((option) => option.value !== 'Safari');
};

/**
 * Returns the supported states for an OS+Browser pair.
 */
export const getSupportedStates = (os, browser) => {
  const states = PREVIEW_VARIANTS[os]?.[browser]?.states;
  return states ? Object.keys(states) : [];
};

/**
 * Given a desired state, returns the best-fit state that is supported.
 */
export const coerceStateForVariant = (os, browser, desiredState) => {
  const supportedStates = getSupportedStates(os, browser);
  if (!supportedStates.length) return null;
  if (supportedStates.includes(desiredState)) return desiredState;
  return supportedStates[0];
};

/**
 * Fetches the resolved variant config for OS+Browser+State.
 * Returns { variant, stateKey, stateConfig } or null if unsupported.
 */
export const getResolvedVariant = (os, browser, state) => {
  const variant = PREVIEW_VARIANTS[os]?.[browser];
  if (!variant) return null;
  const stateKey = coerceStateForVariant(os, browser, state);
  if (!stateKey) return null;
  const stateConfig = variant.states[stateKey];
  return { variant, stateKey, stateConfig };
};


