/**
 *
 * @ai-apps/angular v2.152.0 | _component-tokens.scss
 *
 * Copyright 2014, 2023 IBM
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


//
// Copyright IBM Corp. 2020
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

@import 'vars';
@import 'theme';
@import 'feature-flags';

$enable-css-custom-properties: feature-flag-enabled(
  'enable-css-custom-properties'
);

/// Get a component token value.
/// @access private
/// @param {Map} $tokens - Map of component tokens
/// @param {String} $name - Token name
/// @example get-token-value($component-tokens, 'tag-background-red');
/// @returns {String} Token value
/// @group component-tokens
@function get-token-value($tokens, $name, $force-static-values: false) {
  $emit-as-css-custom-property: $force-static-values == false and
    feature-flag-enabled('enable-css-custom-properties');

  @if map-has-key($tokens, $name) {
    $config: map-get($tokens, $name);
    $fallback: map-get($config, 'fallback');
    $options: map-get($config, 'values');

    @each $option in $options {
      $theme: map-get($option, 'theme');

      @if is-subset-of-theme($carbon--theme, $theme) == true {
        $value: map-get($option, 'value');

        @return if(
          $emit-as-css-custom-property,
          var(--#{$custom-property-prefix}-#{$name}, #{$value}),
          $value
        );
      }
    }

    @return if(
      $emit-as-css-custom-property,
      var(--#{$custom-property-prefix}-#{$name}, #{$fallback}),
      $fallback
    );
  } @else {
    @error 'Unable to find the token #{$name} in the given map';
    @return null;
  }
}

/// Check to see if $b is a subset of $a, meaning that all properties of the
/// $b are defined in $a and have the same value.
/// @access private
/// @param {Map} $a The potential superset of $b
/// @param {Map} $b The potential subset of $a
/// @returns {Boolean}
@function is-subset-of-theme($a, $b) {
  @each $key, $expected in $b {
    $actual: map-get($a, $key);
    @if $expected != $actual {
      @return false;
    }
  }

  @return true;
}

/// @access private
/// @param {Map} $tokens - Map of component tokens
/// @example @include emit-component-tokens($component-tokens);
@mixin emit-component-tokens($tokens) {
  @each $key, $options in $tokens {
    @include custom-property($key, get-token-value($tokens, $key, true));
  }
}
