@use "sass:string";
@use "sass:list";
@use "sass:map";
@import "./theme/@exports";

@function get_mono_color($theme_name, $contrast_name, $opacity: 1) {
  $components_: get_palette_color_component_list_tostr(get_theme($theme_name), "mono", "mono", $contrast_name);

  @if $opacity < 1 {
    $separator_: ",";
    @return string.unquote("hsla(#{$components_ + $separator_ + $opacity})");
  }

  $theme_color_: string.unquote("hsl(#{$components_})");
  @return $theme_color_;
}

@function get_color($theme_name, $color_name, $contrast_name, $opacity: 1, $use_root_variables: true) {
  $components_: get_palette_color_component_list_tostr(get_theme($theme_name), "main", $color_name, $contrast_name, $use_root_variables);

  @if $opacity < 1 {
    $separator_: ",";
    @return string.unquote("hsla(#{$components_ + $separator_ + $opacity})");
  }

  $theme_color_: string.unquote("hsl(#{$components_})");
  @return $theme_color_;
}

@function set_saturation_or_brightness($component_list, $component_item_idx, $amount) {
  @return quote(#{calc(#{list.nth($component_list, $component_item_idx)} + #{$amount})});
}

@function get_custom_color($theme_name, $color_type, $color_name, $contrast_name, $custom-options: ()) {
  $options_: map.merge(("saturation_amount": 0%, "brightness_amount": 0%), $custom-options);
  $string_: "";
  $component_list_: get_palette_color_component_list(get_theme($theme_name), $color_type, $color_name, $contrast_name, true);

  @if $component_list_ and length($component_list_) > 0 {
    @if list.nth($component_list_, 1) and length(list.nth($component_list_, 1)) > 0 {
      $string_: list.nth($component_list_, 1);
    }

    @for $i from 1 to length($component_list_) {
      $separator_: ",";
      @if $i == 2 {
        $string_: $string_ + $separator_ + set_saturation_or_brightness($component_list_, $i + 1, map.get($options_, "brightness_amount"));
      } @else if $color_type != "mono" {
        $string_: $string_ + $separator_ + set_saturation_or_brightness($component_list_, $i + 1, map.get($options_, "saturation_amount"));
      } @else {
        $string_: $string_ + $separator_ + #{list.nth($component_list_, $i + 1)};
      }
    }
  }

  @return string.unquote("hsl(#{$string_})");
}

@function get_custom_maincolor($theme_name, $color_name, $contrast_name, $custom-options: ()) {
  @return get_custom_color($theme_name, "main", $color_name, $contrast_name, $custom-options);
}

@function get_custom_monocolor($theme_name, $color_name, $contrast_name, $custom-options: ()) {
  @return get_custom_color($theme_name, "mono", $color_name, $contrast_name, $custom-options);;
}

@function get_monocolor_description_array($theme_name) {
  $monocolor_description_array_: ();

  @for $contrast_name_dx from 0 to length($CONTRAST_NAMELIST) {
    /** название контраста */
    $contrast_name_: list.nth($CONTRAST_NAMELIST, $contrast_name_dx + 1);

    $monocolor_description_array_: append($monocolor_description_array_, (
      "name": "mono-#{$contrast_name_}",
      "components": get_palette_color_component_list_tostr(get_theme($theme_name), "mono", "mono", $contrast_name_)
    ));
  }

  @return $monocolor_description_array_;
}

@function get_maincolor_description_array($theme_name) {
  $color_description_array_: ();

  @for $color_name_idx from 0 to length($COLOR_NAMELIST) {
    @for $contrast_name_dx from 0 to length($CONTRAST_NAMELIST) {
      /** название цветового варианта */
      $color_name_: list.nth($COLOR_NAMELIST, $color_name_idx + 1);
      /** название контраста */
      $contrast_name_: list.nth($CONTRAST_NAMELIST, $contrast_name_dx + 1);

      $color_description_array_: list.append($color_description_array_, (
        "name": "#{$color_name_}-#{$contrast_name_}",
        "components": get_palette_color_component_list_tostr(get_theme($theme_name), "main", $color_name_, $contrast_name_)
      ));
    }
  }

  @return $color_description_array_;
}
