export const VIEWPORT_ORIENTATIONS = Object.freeze([
  'horizontal', 'vertical', 'square',
]);

export function normalizeViewportOrientation(value) {
  return VIEWPORT_ORIENTATIONS.includes(value) ? value : 'horizontal';
}

export function viewportAspectRatio(orientation) {
  const normalized = normalizeViewportOrientation(orientation);
  if (normalized === 'vertical') return 9 / 16;
  if (normalized === 'square') return 1;
  return 16 / 9;
}

const BASE_RESOLUTIONS = Object.freeze({
  '720': Object.freeze({ width: 1280, height: 720 }),
  '1080': Object.freeze({ width: 1920, height: 1080 }),
  '1440': Object.freeze({ width: 2560, height: 1440 }),
  '2160': Object.freeze({ width: 3840, height: 2160 }),
});

export function exportResolution(value, orientation) {
  const base = BASE_RESOLUTIONS[value] || BASE_RESOLUTIONS['1080'];
  const normalized = normalizeViewportOrientation(orientation);
  if (normalized === 'vertical') {
    return { width: base.height, height: base.width };
  }
  if (normalized === 'square') {
    return { width: base.height, height: base.height };
  }
  return { ...base };
}

export function exportResolutionLabels(orientation) {
  return Object.fromEntries(Object.keys(BASE_RESOLUTIONS).map(value => {
    const { width, height } = exportResolution(value, orientation);
    const name = value === '2160' ? '4K' : `${value}p`;
    return [value, `${name} (${width}×${height})`];
  }));
}
