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

/// @access private
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
@mixin _apply-theme($o3r-theme, $root-name, $details: (tags: ('theme')), $metadata-black-list-pattern: ()) {
  @if (meta.type-of($o3r-theme) != map) {
    @include define-var($root-name, $o3r-theme, $details, $metadata-black-list-pattern);
  } @else {
    @if (theme-functions.is-variable($o3r-theme)) {
      @if (map.has-key($o3r-theme, details)) {
        @include _apply-theme(map.get($o3r-theme, value), $root-name, map.get($o3r-theme, details), $metadata-black-list-pattern);
      } @else {
        @include _apply-theme(map.get($o3r-theme, value), $root-name, $details, $metadata-black-list-pattern);
      }
    } @else {
      @each $key, $value in $o3r-theme {
        $new-key: if($root-name != '', '#{$root-name}-#{$key}', $key);
        @include _apply-theme($value, $new-key, $details, $metadata-black-list-pattern);
      }
    }
  }
}

/// Apply an Otter theme to your application
/// @access public
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @param {@see @link https://github.com/AmadeusITGroup/otter/blob/main/docs/styling/THEME.md#technical-structure-advance} $o3r-theme Otter theme
@mixin apply-theme($o3r-theme: (), $metadata-black-list-pattern: ('_mat-theming-internal')) {
  @warn 'apply-theme is deprecated, prefer to apply css variable directly. Will be removed in v15';
  :root {
    @include _apply-theme($o3r-theme, '', $metadata-black-list-pattern: $metadata-black-list-pattern);
  }
}

/// Override an Otter theme at runtime
/// @param $variable-map Map of variable/override value to override
/// @deprecated Prefer to override css variable directly. Will be removed in v15
@mixin override-theme($variable-map) {
  @warn 'override-theme is deprecated, prefer to override css variable directly. Will be removed in v15';
  :root {
    @each $name, $value in $variable-map {
      $key: utils-functions.get-var-key($name);
      --#{$key}: #{$value};
    }
  }
}

/// Generated CSS Var definition statement and report tags for metadata extraction
/// @access public
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @alias --
/// @param {string} $name Name of the variable
/// @param $value Fallback value
/// @param {map} $tags 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
/// @example
///   @use '@o3r/styling' as o3r
///   :root {
///     @include o3r.define-var('my-scss-var', '#fff', (description: 'My scss variable', tags: 'my-scss-var'))
///   }
///
/// @example
///   @use '@o3r/styling' as o3r
///   :root {
///     @include o3r.define-var('--my-scss-var', '#fff')
///   }
///
/// @example
///   @use '@o3r/styling' as o3r
///   :root {
///     // Override the metadata of an existing "--my-scss-var" variable
///     @include o3r.define-var('--my-scss-var', null, (description: 'My scss variable'))
///   }
@mixin define-var($name, $value, $details, $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);
  @if ($value != null) {
    #{$varName}: #{$value};
  } @else {
    @debug 'Ignored variable #{$varName} value set because the value is defined as "null"';
  }
}

/// Generated CSS Var definition statement and report tags for metadata extraction
/// @access public
/// @deprecated The function is deprecated, rely on CSS Variable strategy instead. Will be removed in v15
/// @alias define-var
/// @param {string} $name Name of the variable
/// @param $value Fallback value
/// @param {map} $tags 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
/// @example
///   @use '@o3r/styling' as o3r
///   :root {
///     @include o3r.var('my-scss-var', '#fff', (description: 'My scss variable', tags: 'my-scss-var'))
///   }
///
/// @example
///   @use '@o3r/styling' as o3r
///   :root {
///     @include o3r.var('my-scss-var', '#fff')
///   }
@mixin var($name, $value, $details, $metadata-black-list-pattern: ()) {
  @include define-var($name, $value, $details, $metadata-black-list-pattern)
}
