///
//  Copyright (c) 2022 GrowStocks
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.
///

@use 'node_modules/@matteusan/sentro';
@use '../tokens';
@use '../tools';
@use 'sass:map';
@use 'theme-tokens' as token;

$expressive-inks: false !default;

@mixin theme-config(
  $surface: (),
  $accent: (),
  $background: (),
  $error: (),
  $warning: (),
  $success: (),
  $undefined: (),
  $radius: (),
  $breakpoints: (),
  $global: (),
  $supertitle: (),
  $title: (),
  $subtitle: (),
  $body: (),
  $mono: (),
  $small: (),
) {

  $_init-radius: (
    'small': 'radius.sm',
    'medium': 'radius.md',
    'large': 'radius.lg',
  );

  $_init-breakpoints: (
    'small': 'breakpoint.sm',
    'medium': 'breakpoint.md',
    'large': 'breakpoint.lg',
    'xlarge': 'breakpoint.xl',
  );

  $_typography-mapper: (
    'global': $global,
    'supertitle': $supertitle,
    'title': $title,
    'subtitle': $subtitle,
    'body': $body,
    'mono': $mono,
    'small': $small,
  );

  @include token.config(
    $surface: _color-theme-token-module($surface),
    $accent: _color-theme-token-module($accent),
    $background: tokens.primitive-token-switch($background),
    $background-ink: tools.get-ink(tokens.primitive-token-switch($background)),
    $error: _color-theme-token-module($error),
    $warning: _color-theme-token-module($warning),
    $success: _color-theme-token-module($success),
    $undefined: _color-theme-token-module($undefined),
    $radius: _shape-theme-token-module(map.merge($_init-radius, $radius)),
    $family: _typography-theme-token-module($_typography-mapper, 'family'),
    $size: _typography-theme-token-module($_typography-mapper, 'size'),
    $weight: _typography-theme-token-module($_typography-mapper, 'weight'),
    $line-height: _typography-theme-token-module($_typography-mapper, 'line-height'),
  );

  @include sentro.breakpoint-config(
    _shape-theme-token-module(map.merge($_init-breakpoints, $breakpoints))
  );
}

/// @access private
@function _color-theme-token-module($query) {
  @if tools.is-type($query, 'map', $errors: false) {
    @return (
      200: tokens.primitive-token-switch('#{map.get($query, 200)}.200', map.get($query, 200)),
      300: tokens.primitive-token-switch('#{map.get($query, 300)}.300', map.get($query, 300)),
      400: tokens.primitive-token-switch('#{map.get($query, 400)}.400', map.get($query, 400)),
      600: tokens.primitive-token-switch('#{map.get($query, 600)}.600', map.get($query, 600)),
      800: tokens.primitive-token-switch('#{map.get($query, 800)}.800', map.get($query, 800)),
      'ink': tools.get-ink(tokens.primitive-token-switch('#{map.get($query, 400)}.400', map.get($query, 400)), $expressive-inks)
    );
  }

  @return (
    200: tokens.primitive-token-get('#{$query}.200'),
    300: tokens.primitive-token-get('#{$query}.300'),
    400: tokens.primitive-token-get('#{$query}.400'),
    600: tokens.primitive-token-get('#{$query}.600'),
    800: tokens.primitive-token-get('#{$query}.800'),
    'ink': tools.get-ink(tokens.primitive-token-get('#{$query}.400'), $expressive-inks)
  );
}

/// @access private
@function _shape-theme-token-module($query) {
  $result: ();
  @each $key, $value in $query {
    $result: map.set($result, $key, tokens.primitive-token-switch($value));
  }
  @return $result;
}

/// @access private
@function _typography-theme-token-module($query, $property) {
  @return (
    'global': tokens.primitive-token-switch(map.get(map.get($query, 'global'), $property)),
    'supertitle': tokens.primitive-token-switch(map.get(map.get($query, 'supertitle'), $property)),
    'title': tokens.primitive-token-switch(map.get(map.get($query, 'title'), $property)),
    'subtitle': tokens.primitive-token-switch(map.get(map.get($query, 'subtitle'), $property)),
    'body': tokens.primitive-token-switch(map.get(map.get($query, 'body'), $property)),
    'mono': tokens.primitive-token-switch(map.get(map.get($query, 'mono'), $property)),
    'small': tokens.primitive-token-switch(map.get(map.get($query, 'small'), $property)),
  );
}