@use 'sass:map';

$required-tokens: primary, secondary, accent, background, surface, hover, border,
  text, muted, code;

$themes: (
  'default': (
    'dark': (
      primary: #ec2fbd,
      secondary: #8616f4,
      accent: #f15bd8,
      background: #160f1e,
      surface: #21172b,
      hover: #30213e,
      border: #49345a,
      text: #f6eff8,
      muted: #c3b1c9,
      code: #edb9ee,
      gradient-middle: #b625e8,
      gradient-end: #7917f6,
    ),
    'light': (
      primary: #c21991,
      secondary: #6812cf,
      accent: #7912aa,
      background: #f4edf4,
      surface: #fff9ff,
      hover: #eaddea,
      border: #d9c7d8,
      text: #30232f,
      muted: #6f5e6d,
      code: #87166f,
      gradient-middle: #a21acb,
      gradient-end: #6d18d8,
    ),
  ),
  'dark-green': (
    'dark': (
      primary: #34d399,
      secondary: #0f9f6e,
      accent: #78ebbd,
      background: #071713,
      surface: #0d241d,
      hover: #14352b,
      border: #285545,
      text: #edfff8,
      muted: #a7cbbb,
      code: #92e9c6,
    ),
    'light': (
      primary: #087f5b,
      secondary: #0b6b4d,
      accent: #096b50,
      background: #edf7f3,
      surface: #f9fffc,
      hover: #dcefe7,
      border: #bdd8cc,
      text: #17362b,
      muted: #506f62,
      code: #087257,
    ),
  ),
  'lime': (
    'dark': (
      primary: #b8f227,
      secondary: #65d30d,
      accent: #d3ff65,
      background: #14190a,
      surface: #20280f,
      hover: #303b17,
      border: #4a5b25,
      text: #f7ffe8,
      muted: #becd9d,
      code: #d6f98a,
    ),
    'light': (
      primary: #527d12,
      secondary: #3f6212,
      accent: #4c7216,
      background: #f5f9e9,
      surface: #fcfff6,
      hover: #e7efd0,
      border: #cedbaa,
      text: #2a351b,
      muted: #65734b,
      code: #557c19,
    ),
  ),
  'orange': (
    'dark': (
      primary: #ff8a3d,
      secondary: #f0561d,
      accent: #ffb36e,
      background: #1c110b,
      surface: #2a1b12,
      hover: #3d281a,
      border: #65422b,
      text: #fff4ec,
      muted: #d3b39e,
      code: #ffc08a,
    ),
    'light': (
      primary: #c2410c,
      secondary: #9a3412,
      accent: #b4440f,
      background: #fff3e9,
      surface: #fffaf5,
      hover: #f8dfca,
      border: #e8c4a8,
      text: #3b271b,
      muted: #7b624f,
      code: #a63f13,
    ),
  ),
  'gold': (
    'dark': (
      primary: #f3c94f,
      secondary: #b98712,
      accent: #ffe08a,
      background: #191409,
      surface: #28200d,
      hover: #3a3015,
      border: #5f4e25,
      text: #fff9e8,
      muted: #cec09c,
      code: #f7d978,
    ),
    'light': (
      primary: #956400,
      secondary: #785000,
      accent: #85600c,
      background: #fbf5e5,
      surface: #fffdf7,
      hover: #f1e6c6,
      border: #ddcea3,
      text: #392f18,
      muted: #75694a,
      code: #805b08,
    ),
  ),
  'crystal': (
    'dark': (
      primary: #52e5ff,
      secondary: #15a8d4,
      accent: #a7f3ff,
      background: #07171d,
      surface: #0d252e,
      hover: #143742,
      border: #285866,
      text: #effdff,
      muted: #a8cbd2,
      code: #9aeaf5,
    ),
    'light': (
      primary: #006f88,
      secondary: #075d73,
      accent: #087186,
      background: #edf8fa,
      surface: #f9feff,
      hover: #dceff2,
      border: #bbd9df,
      text: #17343b,
      muted: #526f76,
      code: #006a80,
    ),
  ),
  'dark-blue': (
    'dark': (
      primary: #5b8cff,
      secondary: #4d5ee8,
      accent: #8eb2ff,
      background: #080f24,
      surface: #101a36,
      hover: #19274b,
      border: #304677,
      text: #f1f5ff,
      muted: #aab8d9,
      code: #a9c4ff,
    ),
    'light': (
      primary: #2459b8,
      secondary: #3648b5,
      accent: #2e5fb0,
      background: #eef3fc,
      surface: #fafcff,
      hover: #dfe8f8,
      border: #c2cee4,
      text: #1d2c49,
      muted: #5a6987,
      code: #315ca4,
    ),
  ),
  'pink': (
    'dark': (
      primary: #f472b6,
      secondary: #db2777,
      accent: #f9a8d4,
      background: #1c0d17,
      surface: #2a1523,
      hover: #3c2032,
      border: #643650,
      text: #fff1f8,
      muted: #d2adbf,
      code: #f7acd0,
    ),
    'light': (
      primary: #b51f68,
      secondary: #9d174d,
      accent: #ad255f,
      background: #fbeef4,
      surface: #fff9fc,
      hover: #f3dce7,
      border: #dfbfd0,
      text: #3b2431,
      muted: #795b6b,
      code: #a92362,
    ),
  ),
);

@function _mode($preset, $mode, $overrides) {
  $theme: map.get($themes, $preset);

  @if $theme == null {
    @error 'Unknown sylar theme preset "#{$preset}". Choose default, dark-green, lime, orange, gold, crystal, dark-blue, or pink.';
  }

  $values: map.merge(map.get($theme, $mode), $overrides);

  @each $token in $required-tokens {
    @if not map.has-key($values, $token) {
      @error 'The sylar #{$mode} theme is missing the "#{$token}" token.';
    }
  }

  @return $values;
}

@mixin _tokens($values, $scheme) {
  $primary: map.get($values, primary);
  $secondary: map.get($values, secondary);
  $accent: map.get($values, accent);
  $background: map.get($values, background);
  $gradient-middle: $accent;
  $gradient-end: $secondary;

  @if map.has-key($values, gradient-middle) {
    $gradient-middle: map.get($values, gradient-middle);
  }

  @if map.has-key($values, gradient-end) {
    $gradient-end: map.get($values, gradient-end);
  }

  color-scheme: $scheme;
  --sylar-primary-bg: color-mix(in srgb, #{$primary} 16%, transparent);
  --sylar-secondary: #{$secondary};
  --sylar-secondary-bg: color-mix(in srgb, #{$secondary} 16%, transparent);
  --sylar-accent-bg: color-mix(in srgb, #{$accent} 16%, transparent);
  --sylar-primary: #{$primary};
  --sylar-accent: #{$accent};
  --sylar-violet: #{$secondary};
  --sylar-gradient: linear-gradient(
    135deg,
    #{$primary} 0%,
    #{$gradient-middle} 48%,
    #{$gradient-end} 100%
  );
  --sylar-bg: #{$background};
  --sylar-surface: #{map.get($values, surface)};
  --sylar-hover: #{map.get($values, hover)};
  --sylar-border: #{map.get($values, border)};
  --sylar-text: #{map.get($values, text)};
  --sylar-muted: #{map.get($values, muted)};
  --sylar-code: #{map.get($values, code)};
  --sylar-header: color-mix(in srgb, #{$background} 90%, transparent);
  --sylar-selection: color-mix(in srgb, #{$primary} 24%, transparent);
  --sylar-glow-primary: color-mix(in srgb, #{$primary} 10%, transparent);
  --sylar-glow-secondary: color-mix(in srgb, #{$secondary} 18%, transparent);
  --app-background: var(--sylar-bg);
}

@mixin apply-theme(
  $preset: 'default',
  $dark-overrides: (),
  $light-overrides: ()
) {
  $dark: _mode($preset, 'dark', $dark-overrides);
  $light: _mode($preset, 'light', $light-overrides);

  :root,
  :root[data-theme='dark'] {
    @include _tokens($dark, dark);
  }

  :root[data-theme='light'] {
    @include _tokens($light, light);
  }
}

// Emits every built-in palette behind a runtime data attribute. The
// documentation application uses this to preview presets without rebuilding;
// consuming applications can continue to compile only their chosen preset.
@mixin apply-theme-presets() {
  @each $preset, $theme in $themes {
    $dark: _mode($preset, 'dark', ());
    $light: _mode($preset, 'light', ());

    :root[data-color-theme='#{$preset}'],
    :root[data-color-theme='#{$preset}'][data-theme='dark'] {
      @include _tokens($dark, dark);
    }

    :root[data-color-theme='#{$preset}'][data-theme='light'] {
      @include _tokens($light, light);
    }
  }
}
