@use 'sass:map';
@use 'sass:meta';

@use '../tokens';

$tokens: meta.module-variables(tokens) !default;

@function getToken($name) {
    @return map.get($tokens, $name);
}

@mixin kbq-css-font-variable($component, $component-property, $font-property, $font) {
    #{$font-property}: var(--kbq-#{$component}-#{$font}#{$component-property}-#{$font-property});
}

@mixin kbq-typography-css-variables($component, $property) {
    @include kbq-css-font-variable($component, $property, font-size, font-);
    @include kbq-css-font-variable($component, $property, font-style, font-);
    @include kbq-css-font-variable($component, $property, font-weight, font-);
    @include kbq-css-font-variable($component, $property, line-height, font-);
    @include kbq-css-font-variable($component, $property, font-family, font-);
    @include kbq-css-font-variable($component, $property, text-transform, font-);
    @include kbq-css-font-variable($component, $property, font-feature-settings, font-);
    @include kbq-css-font-variable($component, $property, letter-spacing, font-);
}

@mixin kbq-typography-level-to-styles-css-variables($component, $property) {
    @include kbq-css-font-variable($component, $property, font-size, '');
    @include kbq-css-font-variable($component, $property, font-style, '');
    @include kbq-css-font-variable($component, $property, font-weight, '');
    @include kbq-css-font-variable($component, $property, line-height, '');
    @include kbq-css-font-variable($component, $property, font-family, '');
    @include kbq-css-font-variable($component, $property, text-transform, '');
    @include kbq-css-font-variable($component, $property, font-feature-settings, '');
    @include kbq-css-font-variable($component, $property, letter-spacing, '');
}

@function kbq-sum-series-css-variables($variables) {
    $sum: 0;
    @each $variable in $variables {
        @if ($sum == 0) {
            @if (meta.type-of($variable) == number or meta.type-of($variable) == calculation) {
                $sum: $variable;
            } @else {
                $sum: var(--kbq-#{$variable});
            }
        } @else {
            @if (meta.type-of($variable) == number or meta.type-of($variable) == calculation) {
                $sum: calc($sum + $variable);
            } @else {
                $sum: calc($sum + var(--kbq-#{$variable}));
            }
        }
    }
    @return $sum;
}

@function kbq-difference-series-css-variables($variables) {
    $difference: 0;
    @each $variable in $variables {
        @if ($difference == 0) {
            @if (meta.type-of($variable) == number or meta.type-of($variable) == calculation) {
                $difference: $variable;
            } @else {
                $difference: var(--kbq-#{$variable});
            }
        } @else {
            @if (meta.type-of($variable) == number or meta.type-of($variable) == calculation) {
                $difference: calc($difference - $variable);
            } @else {
                $difference: calc($difference - var(--kbq-#{$variable}));
            }
        }
    }
    @return $difference;
}

@function kbq-css-half-difference($outer, $inner, $border: null) {
    @if $border {
        @if (meta.type-of($border) == number or meta.type-of($border) == calculation) {
            @return calc((var(--kbq-#{$outer}) - var(--kbq-#{$inner}) - $border * 2) / 2);
        } @else {
            @return calc((var(--kbq-#{$outer}) - var(--kbq-#{$inner}) - var(--kbq-#{$border}) * 2) / 2);
        }
    } @else {
        @return calc((var(--kbq-#{$outer}) - var(--kbq-#{$inner})) / 2);
    }
}
