@use "sass:map";

// Gets the active color's css variable from a variation. Alpha is optional.
// --------------------------------------------------------------------------------------------
// Example usage:
// current-color(base) => var(--ion-color-base)
// current-color(contrast, 0.1) => rgba(var(--ion-color-contrast-rgb), 0.1)
// --------------------------------------------------------------------------------------------
@function current-color($variation, $alpha: null, $subtle: false) {
  $variable: if($subtle, "--ion-color-subtle-#{$variation}", "--ion-color-#{$variation}");

  @if $alpha == null {
    @return var(#{$variable});
  } @else {
    @return rgba(var(#{$variable}-rgb), #{$alpha});
  }
}

// Gets the specific color's css variable from the name and variation. Alpha/rgb are optional.
// --------------------------------------------------------------------------------------------
// Example usage:
// ion-color(primary, base) => var(--ion-color-primary, var(--ion-color-primary-bold))
// ion-color(primary, contrast) => var(--ion-color-primary-contrast, var(--ion-color-primary-bold-contrast))
// ion-color(primary, base, 0.5) => rgba(var(--ion-color-primary-rgb, var(--ion-color-primary-bold-rgb)), 0.5)
// ion-color(primary, base, null, true) => var(--ion-color-primary-rgb, var(--ion-color-primary-bold-rgb))
// ion-color(primary, base, null, null, true) => var(--ion-color-primary-subtle)
// ion-color(primary, foreground, null, null, true) => var(--ion-color-primary-subtle-foreground)
// --------------------------------------------------------------------------------------------
@function ion-color($name, $variation, $alpha: null, $rgb: null, $subtle: false) {
  // Build base variable name
  $base-variable: if($subtle, "--ion-color-#{$name}-subtle", "--ion-color-#{$name}");
  $variation-suffix: if($variation == base, "", "-#{$variation}");
  $variable: "#{$base-variable}#{$variation-suffix}";

  // Build fallback variable name (only for bold colors)
  $fallback-variable: null;
  @if (not $subtle) {
    $fallback-base: "--ion-color-#{$name}-bold";
    $fallback-variable: "#{$fallback-base}#{$variation-suffix}";
  }

  // Handle alpha transparency
  @if ($alpha) {
    $rgb-var: "#{$variable}-rgb";
    $fallback-rgb: if($fallback-variable, "#{$fallback-variable}-rgb", null);

    @if ($fallback-rgb) {
      @return rgba(var(#{$rgb-var}, var(#{$fallback-rgb})), $alpha);
    } @else {
      @return rgba(var(#{$rgb-var}), $alpha);
    }
  }

  // Handle RGB variables
  @if ($rgb) {
    $variable: "#{$variable}-rgb";
    $fallback-variable: if($fallback-variable, "#{$fallback-variable}-rgb", null);
  }

  @if ($fallback-variable) {
    @return var(#{$variable}, var(#{$fallback-variable}));
  } @else {
    @return var(#{$variable});
  }
}

// Mixes a color with black to create its shade.
// --------------------------------------------------------------------------------------------
@function get-color-shade($color) {
  @if (type-of($color) != color) {
    @return $color;
  }
  @return mix(#000, $color, 12%);
}

// Mixes a color with white to create its tint.
// --------------------------------------------------------------------------------------------
@function get-color-tint($color) {
  @if (type-of($color) != color) {
    @return $color;
  }
  @return mix(#fff, $color, 10%);
}

// Converts a color to a comma separated rgb.
// --------------------------------------------------------------------------------------------
@function color-to-rgb-list($color) {
  @if (type-of($color) != color) {
    @return $color;
  }
  @return #{red($color)}, #{green($color)}, #{blue($color)};
}
