@use "sass:list";
@use "sass:map";
@use "../../components/bs5/icons/icons.list" as *;
@use "../../components/bs5/icons/icons-multicolours.list" as *;
@use "../../components/bs5/icons/icons.sizes" as *;
@use "../functions" as f;

$prefix: "qld-" !default;

///
/// @param {String} $name [null] - The name of icon
/// @param {String} $size ["sm"] - The icon size, valid values are "xs" | "sm" | "md" | "lg" | "xl" | "xxl"
/// @param {String | null} $pseudo [null] - Whether the mixin applies styles as a pseudo-element. Valid string values are "before" | "after"
/// @param {Boolean} $is-inline [false] - Applies extra styles to assist with vertical alignent when used inline with text.
/// @param {Boolean} $include-base [true] - Apply all base styles. Set to false when reusing this mixin to create modifier classes where base styles are already applied (For instance qld-icon-{name})
/// @content
@mixin make-icon(
  $name: null,
  $size: "sm",
  $pseudo: null,
  $is-inline: false,
  $include-base: true
) {
  // validate $name
  @if ($name and not f.in-list($name, map.keys($icon-names))) {
    @error "Invalid parameter $name: " + $name;
  }
  // validate $include-base
  @if (not $name and not $include-base and not $is-inline) {
    @error "Must include either $name or $include-base or $is-inline";
  }

  //validate size
  @if (not map.get($icon-sizes, $size)) {
    @error "Invalid parameter $size. Use one of " + map.keys($icon-sizes);
  }

  // validate $pseudo
  @if (not list.index(("before", "after", null), $pseudo)) {
    @error 'Invalid $pseudo value, can only be "before", "after" or null';
  }

  @if $pseudo {
    &::#{$pseudo} {
      content: "";
      // recursively call this mixin with $pseudo set to null
      @include make-icon($name, $size, null, $is-inline, $include-base) {
        @content;
      }
    }
  } @else {
    @if $include-base {
      --_icon-size: var(--icon-size, #{map.get($icon-sizes, $size)});
      display: inline-block;
      width: var(--_icon-size);
      height: var(--_icon-size);
      background-position: center center;
      background-repeat: no-repeat;
      mask-position: center center;
      mask-repeat: no-repeat;
      mask-size: contain;
      background-size: contain;
      vertical-align: middle;

      // What's going on here?
      // These styles unify svg and mask-image implementations
      // Color is the property we can share across both, since we can apply currentColor to backgroundColor.
      // Preferred API to set color on icons is via --qld-icon-color property.
      // This is not set intentionally by default, so we can inherit --qld-action-icon-color from palette context.
      // Final fallback to inherit in case these go wrong.
      color: var(--#{$prefix}icon-color, var(--qld-action-icon-color, inherit));
      background-color: currentColor;
    }
    @if $is-inline {
      vertical-align: baseline;
      position: relative;
      top: calc(calc(var(--_icon-size) / 2) - 0.5cap);
    }
    @if $name {
      @if f.in-list($name, $icons-multicolour) {
        background-image: var(--qgds-icon-#{$name});
        -webkit-background-image: var(--qgds-icon-#{$name});
        background-color: transparent;
      } @else {
        mask-image: var(--qgds-icon-#{$name});
        -webkit-mask-image: var(--qgds-icon-#{$name});
      }
    }

    // allow arbitrary content
    @content;
  }
}
