// Copyright (c) 2016-2018 VMware, Inc. All Rights Reserved.
// This software is released under MIT license.
// The full license information can be found in LICENSE in the root directory of this project.

@function clr-mergeCommonKeys($elMap: (), $valMap: ()) {
    @return map-merge(map-get($elMap, $clr-typography-commonmap-key), $valMap);
}

@mixin clr-getAllTypeKeys($elMap: (), $valMap: ()) {
    $myMapAndCommonMap: clr-mergeCommonKeys($elMap, $valMap);

    @each $key, $value in $myMapAndCommonMap {
        #{$key}: $value;
    }
}

@mixin clr-getTypeKey($valuesToGet: (), $elMap: (), $valMap: ()) {
    @if (length($valuesToGet) > 0) {
        $myMapAndCommonMap: clr-mergeCommonKeys($elMap, $valMap);

        @each $val in $valuesToGet {
            @if map-has-key($myMapAndCommonMap, $val) {
                #{$val}: map-get($myMapAndCommonMap, $val);
            }
        }
    }
}

@mixin clr-getTypePropertiesForDomElement($element-label: null, $typePropertiesToGet: ()) {
    @if ($element-label != null) {
        @if (map-has-key($clr-typography-dom-to-type-element, $element-label)) {
            $myTypographicElement: map-get($clr-typography-dom-to-type-element, $element-label);
            @include clr-getTypeProperties($myTypographicElement, $typePropertiesToGet);
        }
    }
}

@function clr-getTypePropertyValueForDomElement($element-label: null, $valToGet: null) {
    @if not($valToGet) {
        @return "";
    }
    @if not($element-label) {
        @return "";
    }

    @if (map-has-key($clr-typography-dom-to-type-element, $element-label)) {
        $myTypographicElement: map-get($clr-typography-dom-to-type-element, $element-label);
        @return clr-getTypePropertyValue($myTypographicElement, $valToGet);
    }

    @return "";
}

@function clr-getTypePropertyValue($element-label: null, $valToGet: null) {
    @if not($valToGet) {
        @return "";
    }
    @if not($element-label) {
        @return "";
    }

    @if (map-has-key($clr-elements, $element-label)) {
        $myValsContainer: map-get($clr-elements, $element-label);
        $myCommonVals: map-get($myValsContainer, $clr-typography-commonmap-key);

        @if (map-has-key($myCommonVals, $valToGet)) {
            @return map-get($myCommonVals, $valToGet);
        }
    }

    @return "";
}

//Generates responsive typography based on the element style map in variables.clarity.scss
@mixin clr-getTypeProperties(
    $element,
    $whichTypeProperties: (),
    $breakpoint-property: min-width,
    $breakpoints-map: $grid-breakpoints
) {
    //Check if the element exists in the $clr-elements style map
    @if map-has-key($clr-elements, $element) {
        $elMap: map-get($clr-elements, $element); //get the element style map

        @if (length($whichTypeProperties) < 1) {
            // if we don't tell it to go get a typeProperty, we want them all
            @include clr-getAllTypeKeys($elMap);
        } @else {
            @include clr-getTypeKey($whichTypeProperties, $elMap);
        }

        // NOTE: don't want this code to go away because one day we may support responsive font-sizes
        // FIXIT: keeping here. needs much work/refactor to be functional again. but general idea is still here.
        // @each $breakpoint, $val-map in $el-map {
        //     //If min-width < small, no media query as this is the default
        //     @if $breakpoint == null {
        //         @include clr-getAllTypeKeys($val-map, $el-map);
        //     }
        //     //Everything else except the common properties has a media query
        //     @else {
        //         @if $breakpoint != "clr-common" and map-has-key($grid-breakpoints, $breakpoint) {
        //             $breakpoint: map-get($grid-breakpoints, $breakpoint);
        //             @media screen and (# {$breakpoint-property}: $breakpoint) {
        //                 @include clr-getAllTypeKeys($val-map, $el-map);
        //             }
        //         }
        //     }
        // }
    }
}
