/**
 * The pixel frame the cropper presents, per display mode.
 *
 * Pulled out of `CropModal` as a pure function so the three branches can be
 * tested: the opportunity branch used to hardcode a 3.5 banner ratio and ignore
 * the caller's dimensions while the cards rendered the result square, which
 * meant the user framed a wide strip and the card centre-cropped it again.
 * Tests pin that the caller's ratio is honoured now.
 */
export interface CropFrame {
  width: number;
  height: number;
  aspectRatio: number;
}

export function getBaseFrameDimensions({
  cropDisplayMode,
  cropWidth,
  cropHeight,
  aspectRatio,
}: {
  cropDisplayMode?: "opportunity" | "license" | "profile";
  cropWidth?: number;
  cropHeight?: number;
  aspectRatio: number;
}): CropFrame {
  const defaultWidth = 400;
  if (cropDisplayMode === 'opportunity') {
    /*
     * Opportunity, event and learn & serve photos are rendered on cards as a
     * square, so that is what the cropper must produce.
     *
     * Whatever the ratio is, the rule is that this one and the cards' agree.
     * This branch once hardcoded 3.5 (a wide banner) and ignored
     * cropWidth/cropHeight entirely while the cards rendered square — three
     * ratios in one pipeline, so the user cropped a wide strip and the card
     * centre-cropped it again, throwing away most of what they had framed.
     * It honours the caller's dimensions now, and every call site passes the
     * cards' ratio.
     */
    const cardAspect =
      cropWidth && cropHeight ? cropWidth / cropHeight : 1;
    const width = cropWidth || defaultWidth;
    return {
      width,
      height: Math.round(width / cardAspect),
      aspectRatio: cardAspect,
    };
  } else if (cropDisplayMode === 'license') {
    const width = cropWidth || 300;
    return { width, height: width, aspectRatio: 1 };
  }
  const providedAspect = cropWidth && cropHeight ? cropWidth / cropHeight : aspectRatio;
  const height = cropWidth ? Math.round(cropWidth / providedAspect) : Math.round(defaultWidth / providedAspect);
  return {
    width: cropWidth || defaultWidth,
    height: cropHeight || height,
    aspectRatio: providedAspect
  };
}
