@import 'arw-palette';

// For a given hue in a palette, return the contrast color from the map of contrast palettes.
// @param $color-map
// @param $hue
@function wpt-contrast($palette, $hue) {
  @return map-get(map-get($palette, contrast), $hue);
}

// Creates a map of hues to colors for a theme. This is used to define a theme palette in terms
// of the Arrow UI Design hues.
// @param $color-map
// @param $primary
// @param $lighter
@function arw-palette(
  $base-palette,
  $default: 500,
  $lighter: 100,
  $darker: 700
) {
  $result: map_merge(
    $base-palette,
    (
      default: map-get($base-palette, $default),
      lighter: map-get($base-palette, $lighter),
      darker: map-get($base-palette, $darker),
      default-contrast: wpt-contrast($base-palette, $default),
      lighter-contrast: wpt-contrast($base-palette, $lighter),
      darker-contrast: wpt-contrast($base-palette, $darker)
    )
  );

  // For each hue in the palette, add a "-contrast" color to the map.
  @each $hue, $color in $base-palette {
    $result: map_merge(
      $result,
      (
        '#{$hue}-contrast': wpt-contrast($base-palette, $hue)
      )
    );
  }

  @return $result;
}

// Gets a color from a theme palette (the output of arw-palette).
// The hue can be one of the standard values (500, A400, etc.), one of the three preconfigured
// hues (default, lighter, darker), or any of the aforementioned prefixed with "-contrast".
//
// @param $color-map The theme palette (output of arw-palette).
// @param $hue The hue from the palette to use. If this is a value between 0 and 1, it will
//     be treated as opacity.
// @param $opacity The alpha channel value for the color.
@function wpt-color($palette, $hue: default, $opacity: null) {
  // If hueKey is a number between zero and one, then it actually contains an
  // opacity value, so recall this function with the default hue and that given opacity.
  @if type-of($hue) == number and $hue >= 0 and $hue <= 1 {
    @return wpt-color($palette, default, $hue);
  }

  $color: map-get($palette, $hue);
  $opacity: if($opacity == null, opacity($color), $opacity);

  @return rgba($color, $opacity);
}

// Creates a container object for a light theme to be given to individual component theme mixins.
@function wpt-light-theme(
  $primary,
  $secondary,
  $accent,
  $warn: arw-palette($arw-red)
) {
  @return (
    primary: $primary,
    secondary: $secondary,
    accent: $accent,
    warn: $warn,
    is-dark: false,
    foreground: $arw-light-theme-foreground,
    background: $arw-light-theme-background
  );
}

// Creates a container object for a dark theme to be given to individual component theme mixins.
@function wpt-dark-theme(
  $primary,
  $secondary,
  $accent,
  $warn: arw-palette($arw-red)
) {
  @return (
    primary: $primary,
    secondary: $secondary,
    accent: $accent,
    warn: $warn,
    is-dark: true,
    foreground: $arw-dark-theme-foreground,
    background: $arw-dark-theme-background
  );
}
