///
//  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 '../kardia';
@use 'sass:map';
@use 'sass:meta';
@use 'sass:list';
@use 'consts';
@use 'util-helpers' as helpers;

$_breakpoints: consts.$breakpoints;
$_presets: (
  'supertitle': (
    family: 'supertitle',
    size: 'supertitle',
    weight: 'bold',
    line-height: 'normal'
  ),
  'title': (
    family: 'title',
    size: 'title',
    weight: 'bold',
    line-height: 'normal'
  ),
  'subtitle': (
    family: 'subtitle',
    size: 'subtitle',
    weight: 'bold',
    line-height: 'normal'
  ),
  'body': (
    family: 'body',
    size: 'body',
    weight: 'normal',
    line-height: 'normal'
  ),
  'mono': (
    family: 'mono',
    size: 'mono',
    weight: 'normal',
    line-height: 'normal'
  ),
  'small': (
    family: 'small',
    size: 'small',
    weight: 'light',
    line-height: 'normal'
  )
);
$_sizes: ('xs', 'sm', 'md', 'lg', 'xl', '2xl', '3xl', '4xl', '5xl', '6xl');
$_weights: ('black', 'bold', 'normal', 'light');
$_line-heights: ('broad', 'normal', 'condensed');

@mixin typography($include: (), $exclude: ()) {
  $utils-to-render: helpers.handle-include-exclude((
    'preset',
    'family',
    'size',
    'weight',
    'line-height',
  ), $include, $exclude);

  @if list.index($utils-to-render, 'preset') {
    @each $preset, $values in $_presets {
      .#{$preset} {
        font-family: kardia.token-get('family-#{map.get($values, 'family')}');
        font-size: kardia.token-get('size-#{map.get($values, 'size')}');
        font-weight: kardia.primitive-token-get('weight.#{map.get($values, 'weight')}');
        line-height: kardia.primitive-token-get('line-height.#{map.get($values, 'line-height')}');
      }

      @each $breakpoint in $_breakpoints {
        .\@#{$breakpoint}\:#{$preset} {
          @include kardia.breakpoint-create($breakpoint) {
            font-family: kardia.token-get('family-#{map.get($values, 'family')}') !important;
            font-size: kardia.token-get('size-#{map.get($values, 'size')}') !important;
            font-weight: kardia.primitive-token-get('weight.#{map.get($values, 'weight')}') !important;
            line-height: kardia.primitive-token-get('line-height.#{map.get($values, 'line-height')}') !important;
          }
        }
      }
    }
  }

  @if list.index($utils-to-render, 'family') {
    @each $preset, $values in $_presets {
      .family-#{$preset} {
        font-family: kardia.token-get('family-#{$preset}');
      }

      @each $breakpoint in $_breakpoints {
        .\@#{$breakpoint}\:family-#{$preset} {
          @include kardia.breakpoint-create($breakpoint) {
            font-family: kardia.token-get('family-#{$preset}') !important;
          }
        }
      }
    }
  }

  @if list.index($utils-to-render, 'size') {
    @each $size in $_sizes {
      .size-#{$size} {
        font-size: kardia.primitive-token-get('size.#{$size}');
      }

      @each $breakpoint in $_breakpoints {
        .\@#{$breakpoint}\:size-#{$size} {
          @include kardia.breakpoint-create($breakpoint) {
            font-size: kardia.primitive-token-get('size.#{$size}') !important;
          }
        }
      }
    }
  }

  @if list.index($utils-to-render, 'weight') {
    @each $weight in $_weights {
      .weight-#{$weight} {
        font-weight: kardia.primitive-token-get('weight.#{$weight}');
      }

      @each $breakpoint in $_breakpoints {
        .\@#{$breakpoint}\:weight-#{$weight} {
          @include kardia.breakpoint-create($breakpoint) {
            font-weight: kardia.primitive-token-get('weight.#{$weight}') !important;
          }
        }
      }
    }
  }

  @if list.index($utils-to-render, 'line-height') {
    @each $line-height in $_line-heights {
      .line-height-#{$line-height} {
        line-height: kardia.primitive-token-get('line-height.#{$line-height}');
      }

      @each $breakpoint in $_breakpoints {
        .\@#{$breakpoint}\:line-height-#{$line-height} {
          @include kardia.breakpoint-create($breakpoint) {
            line-height: kardia.primitive-token-get('line-height.#{$line-height}') !important;
          }
        }
      }
    }
  }
}