@use 'sass:map';
@use 'sass:meta';
@use 'sass:list';
@use '../utils/functions' as utils-functions;

/// Get an Otter variable reference
/// @access public
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @alias var
/// @param {string} $name Name of the variable
/// @param $value Fallback value
/// @param {map} $details map with `description` as key and with a value a string, `tags` as key and with value a list or single tags to indicate to the cms extractors
/// @return {string} CSS variable
/// @example
///   @use '@o3r/styling' as o3r
///   $my-scss-var: o3r.variable('my-scss-var', '#fff', (description: 'My scss variable', tags: 'my-scss-var'));
@function variable($name, $value: null, $details: null, $metadata-black-list-pattern: ()) {
  $key: utils-functions.get-var-key($name);
  $varName: '--#{$key}';

  $logged: utils-functions.log-variable($key, $value, $details, $metadata-black-list-pattern);
  $res: if($value != null, 'var(#{$varName}, #{$value})', 'var(#{$varName})');
  @return #{$res};
}

/// Get an Otter variable reference
/// @access public
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @alias variable
/// @param {string} $name Name of the variable
/// @param $value Fallback value
/// @param {map} $details map with `description` as key and with a value a string, `tags` as key and with value a list or single tags to indicate to the cms extractors
/// @return {string} CSS variable
/// @example
///   @use '@o3r/styling' as o3r
///   $my-scss-var: o3r.var('my-scss-var', '#fff', (description: 'My scss variable', tags: 'my-scss-var'));
@function var($name, $value: null, $details: null, $metadata-black-list-pattern: ()) {
  @return variable($name, $value, $details, $metadata-black-list-pattern);
}

/// Returns true if the given map is an Otter Theme variable
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @param {map} $map map to test
/// @return {bool} is an Otter compatible variable
@function is-variable($map) {
  @if (meta.type-of($map) != map) {
    @return false;
  } @else {
    $keys: map.keys($map);

    $hasInvalid: false;
    @each $key in $keys {
      @if (not $hasInvalid) {
        $isValidItem: false;
        @each $valid in ('value', 'details', 'variable') {
          @if ($key == $valid) {
            $isValidItem: true;
          }
        }
        $hasInvalid: not $isValidItem;
      }
    }
    @return list.length($keys) >= 1 and not $hasInvalid;
  }
}
