@use 'sass:color';
@use 'sass:map';
@use 'colors' as *;

// ------------------------------------------------------------------ helpers --
/// @param {Color} $color - Source colour.
/// @returns {String} Comma separated channels, for legacy `rgba()`.
@function channels($color) {
  @return #{color.channel($color, 'red', $space: rgb)},
    #{color.channel($color, 'green', $space: rgb)}, #{color.channel($color, 'blue', $space: rgb)};
}

/// @param {Color} $color - Source colour.
/// @returns {String} Space separated channels, for `rgb(var(--x) / a)`.
@function triplet($color) {
  @return #{color.channel($color, 'red', $space: rgb)}
    #{color.channel($color, 'green', $space: rgb)} #{color.channel($color, 'blue', $space: rgb)};
}

@mixin theme-properties($theme) {
  @each $name, $value in $theme {
    --ui-#{$name}: #{$value};
    --ui-#{$name}-rgb: #{triplet($value)};
  }
}

$alphas: (
  'hairline': 0.07,
  'hairline-strong': 0.12,
  'grid-line': 0.022,
  'overlay-1': 0.02,
  'overlay-2': 0.05,
) !default;

/// @param {Map} $alphas - Name to alpha map.
@mixin alpha-properties($alphas) {
  @each $name, $alpha in $alphas {
    --ui-#{$name}: rgb(var(--ui-heading-rgb) / #{$alpha});
  }
}

/// @param {String} $theme - `dark` or `light`.
@mixin scrim-properties($theme) {
  --ui-scrim-base: #{triplet(scrim($theme, 'base'))};
  --ui-scrim-caption: #{scrim($theme, 'caption')};
  --ui-scrim-indicator: #{triplet(scrim($theme, 'indicator'))};
  --ui-scrim-ink: #{scrim($theme, 'ink')};
}

/// Bridges the active theme into Bootstrap's own custom properties.
///
/// @param {Map} $theme - Resolved theme map.
@mixin bootstrap-bridge($theme) {
  --bs-body-bg: var(--ui-canvas);
  --bs-body-bg-rgb: #{channels(map.get($theme, 'canvas'))};
  --bs-body-color: var(--ui-text);
  --bs-body-color-rgb: var(--ui-text-rgb);
  --bs-emphasis-color: var(--ui-heading);
  --bs-emphasis-color-rgb: var(--ui-heading-rgb);
  --bs-secondary-color: var(--ui-text-muted);
  --bs-secondary-bg: var(--ui-surface);
  --bs-secondary-bg-rgb: #{channels(map.get($theme, 'surface'))};
  --bs-tertiary-bg: var(--ui-surface-raised);
  --bs-tertiary-bg-rgb: #{channels(map.get($theme, 'surface-raised'))};
  --bs-border-color: var(--ui-border);
  --bs-border-color-translucent: var(--ui-hairline);
  --bs-heading-color: var(--ui-heading);
  --bs-link-color: var(--ui-accent);
  --bs-link-color-rgb: var(--ui-accent-rgb);
  --bs-link-hover-color: var(--ui-accent-soft);
  --bs-link-hover-color-rgb: var(--ui-accent-soft-rgb);
  --bs-link-opacity: 1;
  --bs-link-hover-opacity: 1;
  --bs-code-color: var(--ui-info);
  --bs-highlight-bg: rgb(var(--ui-warn-rgb) / 0.24);

  @each $name, $token in $subtle-tokens {
    --bs-#{$name}: var(--ui-#{$token});
    --bs-#{$name}-rgb: #{channels(map.get($theme, $token))};
  }

  --bs-light: #{palette('slate-050')};
  --bs-light-rgb: #{channels(palette('slate-050'))};
  --bs-dark: #{palette('slate-900')};
  --bs-dark-rgb: #{channels(palette('slate-900'))};

  @include bootstrap-subtle-bridge();
}

@mixin bootstrap-subtle-bridge() {
  @each $name, $token in $subtle-tokens {
    --bs-#{$name}-bg-subtle: rgb(var(--ui-#{$token}-rgb) / 0.12);
    --bs-#{$name}-text-emphasis: var(--ui-#{$token});
    --bs-#{$name}-border-subtle: rgb(var(--ui-#{$token}-rgb) / 0.32);
  }

  --bs-light-bg-subtle: var(--ui-surface-raised);
  --bs-light-text-emphasis: var(--ui-heading);
  --bs-light-border-subtle: var(--ui-border);

  --bs-dark-bg-subtle: var(--ui-canvas);
  --bs-dark-text-emphasis: var(--ui-text);
  --bs-dark-border-subtle: var(--ui-border-strong);
}

@each $name, $token in $subtle-tokens {
  @include tone-list-group-class($name, $token);
}

// ------------------------------------------------------------------ shared --
:root {
  --ui-glass-saturate: 180%;
  --ui-ease: cubic-bezier(0.22, 1, 0.36, 1);
  --ui-duration: 160ms;

  --ui-table-header-ink: var(--bs-table-color);
  --ui-neutral-light: #{palette('slate-050')};
  --ui-neutral-light-rgb: #{triplet(palette('slate-050'))};
  --ui-warn-bright: #{palette('amber-500')};
  --ui-btn-mix: #{palette('ink-900')};
  --ui-on-neutral-light: #{palette('slate-950')};
  --ui-neutral-dark: #{palette('slate-900')};
  --ui-neutral-dark-rgb: #{triplet(palette('slate-900'))};
  --ui-on-neutral-dark: #{palette('slate-050')};
  --ui-shade-rgb: #{triplet(palette('ink-000'))};
}

// -------------------------------------------------------------------- dark --
:root,
[data-bs-theme='dark'] {
  color-scheme: dark;

  @include theme-properties($theme-dark);
  @include alpha-properties($alphas);
  @include scrim-properties('dark');

  @each $name, $value in $on-dark {
    --ui-on-#{$name}: #{$value};
  }

  --ui-shadow-rgb: #{triplet(map.get($shadow, 'dark'))};

  @include bootstrap-bridge($theme-dark);
}

// ------------------------------------------------------------------- light --
[data-bs-theme='light'] {
  color-scheme: light;

  @include theme-properties($theme-light);
  @include alpha-properties($alphas);
  @include scrim-properties('light');

  @each $name, $value in $on-light {
    --ui-on-#{$name}: #{$value};
  }

  --ui-shadow-rgb: #{triplet(map.get($shadow, 'light'))};

  @include bootstrap-bridge($theme-light);
}
