/* stylelint-disable scss/dollar-variable-pattern */
/* stylelint-disable scss/no-global-function-names */
/* stylelint-disable value-keyword-case */
@use "sass:math";

@function multiply($fore, $back) {
    $red: red($back) * red($fore) / 255;
    $green: green($back) * green($fore) / 255;
    $blue: blue($back) * blue($fore) / 255;

    @return rgb($red, $green, $blue);
}

@function get-color-luminance($color) {
  $colors: (
    'red': red($color),
    'green': green($color),
    'blue': blue($color)
  );

  @each $name, $value in $colors {
    $adjusted: 0;
    $value: $value / 255;

    @if $value < 0.0393 {
      $value: $value / 12.92;
    } @else {
      $value: ($value + .055) / 1.055;
      $value: math.pow($value, 2.4);
    }

    $colors: map-merge($colors, ($name: $value));
  }

  @return (map-get($colors, 'red') * .2126) + (map-get($colors, 'green') * .7152) + (map-get($colors, 'blue') * .0722);
}

@function create-text-color($color, $color-type: 'primary') {
    $red: red($color);
    $green: green($color);
    $blue: blue($color);
    $light-text: $light-primary-text;
    $dark-text: $dark-primary-text;

    @if $color-type == 'accent' {
        $light-text: $light-secondary-text;
        $dark-text: $dark-secondary-text;
    }

    $light-text-luminance: get-color-luminance($light-text);
    $dark-text-luminance: get-color-luminance($dark-text);
    $background-color-luminance: get-color-luminance($color);
    $light-text-luminance: $light-text-luminance + 0.5;
    $dark-text-luminance: $dark-text-luminance + 0.5;
    $background-color-luminance: $background-color-luminance + 0.5;
    $luminance-contrast-for-light-text: $light-text-luminance / $background-color-luminance;
    $luminance-contrast-for-dark-text: $background-color-luminance / $dark-text-luminance;
    $text-colour: $light-text;

    @if $luminance-contrast-for-dark-text > $luminance-contrast-for-light-text {
        $text-colour: $dark-text;
    }

    @return $text-colour;
}

@function create-color-palette($color, $color-type: 'primary') {
    $light: #fff;
    $dark: multiply($color, $color);
    $color50: mix($light, $color, 88%);
    $color100: mix($light, $color, 70%);
    $color200: mix($light, $color, 50%);
    $color300: mix($light, $color, 30%);
    $color400: mix($light, $color, 15%);
    $color500: mix($light, $color, 0%);
    $color600: mix($dark, $color, 13%);
    $color700: mix($dark, $color, 30%);
    $color800: mix($dark, $color, 46%);
    $color900: mix($dark, $color, 75%);
    $colorA100: lighten(saturate(mix($dark, $color, 15%), 80%), 45.6%);
    $colorA200: lighten(saturate(mix($dark, $color, 15%), 80%), 35.6%);
    $colorA400: lighten(saturate(mix($dark, $color, 15%), 100%), 25.6%);
    $colorA700: lighten(saturate(mix($dark, $color, 15%), 100%), 20.5%);
    $contrast50: create-text-color($color50, $color-type);
    $contrast100: create-text-color($color100, $color-type);
    $contrast200: create-text-color($color200, $color-type);
    $contrast300: create-text-color($color300, $color-type);
    $contrast400: create-text-color($color400, $color-type);
    $contrast500: create-text-color($color500, $color-type);
    $contrast600: create-text-color($color600, $color-type);
    $contrast700: create-text-color($color700, $color-type);
    $contrast800: create-text-color($color800, $color-type);
    $contrast900: create-text-color($color900, $color-type);
    $contrastA100: create-text-color($colorA100, $color-type);
    $contrastA200: create-text-color($colorA200, $color-type);
    $contrastA400: create-text-color($colorA400, $color-type);
    $contrastA700: create-text-color($colorA700, $color-type);
    $palette: (
        50: $color50,
        100: $color50,
        200: $color50,
        300: $color50,
        400: $color400,
        500: $color500,
        600: $color600,
        700: $color700,
        800: $color800,
        900: $color900,
        A100: $colorA100,
        A200: $colorA200,
        A400: $colorA400,
        A700: $colorA700,
        contrast: (
            50: $contrast50,
            100: $contrast100,
            200: $contrast200,
            300: $contrast300,
            400: $contrast400,
            500: $contrast500,
            600: $contrast600,
            700: $contrast700,
            800: $contrast800,
            900: $contrast900,
            A100: $contrastA100,
            A200: $contrastA200,
            A400: $contrastA400,
            A700: $contrastA700
        )
    );

    @return $palette;
}
