@use 'sass:list';
@use 'sass:math';
@use 'sass:meta';
@use 'config' as *;

/// The aspect ratios that are used to generate corresponding aspect ratio
/// classes in code
/// @type List
/// @access public
/// @group @cds/grid
$aspect-ratios: (
  (16, 9),
  (9, 16),
  (2, 1),
  (1, 2),
  (4, 3),
  (3, 4),
  (3, 2),
  (2, 3),
  (1, 1)
);

/// Generates the CSS classname utilities for the aspect ratios
///
/// CSS Tricks article on aspect ratios and all the different ways it can be done.
/// https://css-tricks.com/aspect-ratio-boxes/#article-header-id-6
///
/// That article references an earlier article on the topic.
/// https://keithjgrant.com/posts/2017/03/aspect-ratios/
///
/// @param {Number} $width width from an aspect ratio
/// @param {Number} $height height from an aspect ratio
/// @access private
/// @group @cds/grid
@mixin aspect-ratio($aspect-ratios: $aspect-ratios) {
  .#{$prefix}-aspect-ratio {
    position: relative;
  }

  .#{$prefix}-aspect-ratio::before {
    width: 1px;
    height: 0;
    margin-left: -1px;
    content: '';
    float: left;
  }

  .#{$prefix}-aspect-ratio::after {
    display: table;
    clear: both;
    content: '';
  }

  @each $aspect-ratio in $aspect-ratios {
    $width: list.nth($aspect-ratio, 1);
    $height: list.nth($aspect-ratio, 2);

    .#{$prefix}-aspect-ratio--#{$width}x#{$height}::before {
      padding-top: math.percentage(math.div($height, $width));
    }
  }
}
