@use "sass:map";
@use "sass:meta";
@use "../../lib/mixin";
@use "../../settings";

/*
Theme

The theme module.

Weight: -99

Style guide: #{settings.$prefix}-button.theme
*/

@if meta.type-of(settings.$builtin-themes) == "map" {
  /*
Modifiers

The theme modifiers.

Weight: -100

Markup: <!-- with anchor element -->
<a class="#{settings.$prefix}-button  {{modifier_class}}" href="#">
  <span class="#{settings.$prefix}-button__label  #{settings.$prefix}-text">Lorem ipsum dolor sit amet.</span>
</a>
<!-- with button element -->
<button class="#{settings.$prefix}-button  {{modifier_class}}">
  <span class="#{settings.$prefix}-button__label  #{settings.$prefix}-text">あのイーハトーヴォのすきとおった風。</span>
</button>

#{settings.$prefix}-button--theme-normal - Normal theme

Style guide: #{settings.$prefix}-button.theme.builtin
*/
  .#{settings.$prefix}-button--theme {
    $pkg: settings.$pkg;

    &-normal {
      $default-theme: map.get(settings.$builtin-themes, "light");

      // Load colors
      --color-up: rgb(var(--#{$pkg}-color-#{map.get($default-theme, "color")}));
      --color-focus: rgb(
        var(--#{$pkg}-color-#{map.get($default-theme, "color-focus")})
      );
      --background-color-up: rgb(
        var(--#{$pkg}-color-#{map.get($default-theme, "background")})
      );
      --background-color-focus: rgb(
        var(--#{$pkg}-color-#{map.get($default-theme, "background-focus")})
      );
      --border-color-up: rgb(
        var(--#{$pkg}-color-#{map.get($default-theme, "border")})
      );
      --border-color-focus: rgb(
        var(--#{$pkg}-color-#{map.get($default-theme, "border-focus")})
      );

      // Default settings
      --padding: 10px;
      --border-radius: 3px;
      --border-color: var(--border-color-up);
      --transition: background-color 0.1s ease-out;
      --color: var(--color-up);
      --background-color: var(--background-color-up);
      --border: 1px solid var(--border-color);

      &:where(a[href], button) {
        @include mixin.on-focus() {
          --color: var(--color-focus);
          --background-color: var(--background-color-focus);
          --border-color: var(--border-color-focus);
        }
      }

      // Settings by color schemes
      @each $name, $theme in settings.$builtin-themes {
        @media (prefers-color-scheme: #{$name}) {
          $color-up: map.get($theme, "color");
          $color-focus: map.get($theme, "color-focus");
          $background-up: map.get($theme, "background");
          $background-focus: map.get($theme, "background-focus");
          $border-up: map.get($theme, "border");
          $border-focus: map.get($theme, "border-focus");

          // Apply colors to settings
          --color-up: rgb(var(--#{$pkg}-color-#{$color-up}));
          --color-focus: rgb(var(--#{$pkg}-color-#{$color-focus}));
          --background-color-up: rgb(var(--#{$pkg}-color-#{$background-up}));
          --background-color-focus: rgb(
            var(--#{$pkg}-color-#{$background-focus})
          );
          --border-color-up: rgb(var(--#{$pkg}-color-#{$border-up}));
          --border-color-focus: rgb(var(--#{$pkg}-color-#{$border-focus}));
        }
      }
    }
  }
}
