// =============================================================================
// Form tone mixins
// =============================================================================

@use 'sass:string';
@use 'sass:map';
@use 'sass:color';
@use 'sass:meta';

/// Picks black or white for a mark drawn on top of `$background`.
///
/// Uses the WCAG relative luminance coefficients, so the result matches the
/// perceived brightness instead of the naive channel average.
///
/// @param {Color} $background - Colour the mark is painted on.
/// @return {Color} `#000000` or `#ffffff`.
@function form-mark-contrast($background) {
  @if meta.type-of($background) != 'color' {
    @error 'Expected a colour, got `#{meta.type-of($background)}`.';
  }

  $luminance: color.channel($background, 'red', $space: rgb) * 0.2126 +
    color.channel($background, 'green', $space: rgb) * 0.7152 +
    color.channel($background, 'blue', $space: rgb) * 0.0722;

  @return if($luminance > 140, #000000, #ffffff);
}

/// Builds the `background-image` used by a checked checkbox.
///
/// Accepts a colour or a hex string, so it can be fed either by a palette
/// entry or by `form-mark-contrast()`.
///
/// @param {Color|String} $color - Hex colour, with or without the leading `#`.
/// @return {String} A `url()` with the URL-encoded SVG.
@function form-mark-url($color) {
  $value: #{$color};

  @if string.slice($value, 1, 1) != '#' {
    @error 'Expected a hex colour, got `#{$color}`.';
  }

  $hex: string.slice($value, 2);

  @if string.length($hex) != 6 {
    @error 'Expected a 6-digit hex colour, got `#{$color}`.';
  }

  @return url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23#{$hex}' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
}

/// Re-points the three `--form-*` properties to a tone.
///
/// @param {String} $tone - Token suffix, without the `--ui-` prefix.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-tone-properties($tone, $on: $tone) {
  --form-tone: var(--ui-#{$tone});
  --form-tone-rgb: var(--ui-#{$tone}-rgb);
  --form-tone-on: var(--ui-on-#{$on});
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-select-tone($name, $tone: $name, $on: $tone) {
  .form-select-#{$name} {
    @include form-tone-properties($tone, $on);
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
/// @param {String|null} $mark - Hex colour of the checkmark. `null` keeps the
///   Bootstrap default (white).
@mixin form-check-tone($name, $tone: $name, $on: $tone, $mark: null) {
  .form-check-#{$name},
  .form-check-input-#{$name} {
    @include form-tone-properties($tone, $on);
  }

  @if $mark != null {
    $mark-url: form-mark-url($mark);

    .form-check-#{$name}:not(.form-switch) .form-check-input:checked[type='checkbox'],
    .form-check-input-#{$name}:checked[type='checkbox']:not(.form-switch .form-check-input) {
      background-image: $mark-url;
    }
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-range-tone($name, $tone: $name, $on: $tone) {
  .form-range-#{$name} {
    @include form-tone-properties($tone, $on);
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-control-tone($name, $tone: $name, $on: $tone) {
  .form-control-#{$name} {
    @include form-tone-properties($tone, $on);

    border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);

    &:hover:not(:disabled):not(:focus) {
      border-color: color-mix(in srgb, var(--form-tone) 65%, transparent);
    }
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-control-color-tone($name, $tone: $name, $on: $tone) {
  .form-control-color-#{$name} {
    @include form-tone-properties($tone, $on);

    border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);

    &::-webkit-color-swatch-wrapper {
      border-radius: inherit;
    }

    &::-webkit-color-swatch {
      border: 0;
      border-radius: calc(var(--bs-border-radius-sm) - 2px);
    }

    &:hover:not(:disabled):not(:focus) {
      border-color: color-mix(in srgb, var(--form-tone) 65%, transparent);
    }
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-file-tone($name, $tone: $name, $on: $tone) {
  .form-file-#{$name} {
    @include form-tone-properties($tone, $on);

    border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);

    &::file-selector-button {
      color: var(--form-tone);
      background-color: rgb(var(--form-tone-rgb) / 0.12);
      border-inline-end: 1px solid color-mix(in srgb, var(--form-tone) 45%, transparent);
      transition: background-color var(--ui-duration) var(--ui-ease);
    }

    &:hover:not(:disabled)::file-selector-button {
      background-color: rgb(var(--form-tone-rgb) / 0.2);
    }

    &:focus {
      border-color: var(--form-tone);
      box-shadow: 0 0 0 3px rgb(var(--form-tone-rgb) / 0.2);
    }
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-input-group-tone($name, $tone: $name, $on: $tone) {
  .input-group-#{$name} {
    @include form-tone-properties($tone, $on);

    > .input-group-text {
      color: var(--form-tone);
      background-color: rgb(var(--form-tone-rgb) / 0.12);
      border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);
    }

    > .form-control,
    > .form-select {
      border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);

      &:focus {
        border-color: var(--form-tone);
        box-shadow: 0 0 0 3px rgb(var(--form-tone-rgb) / 0.2);
      }
    }
  }
}

/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
@mixin form-floating-tone($name, $tone: $name, $on: $tone) {
  .form-floating-#{$name} {
    @include form-tone-properties($tone, $on);

    > .form-control,
    > .form-select {
      border-color: color-mix(in srgb, var(--form-tone) 45%, transparent);

      &:focus {
        border-color: var(--form-tone);
        box-shadow: 0 0 0 3px rgb(var(--form-tone-rgb) / 0.2);
      }
    }

    > label {
      color: color-mix(in srgb, var(--form-tone) 70%, var(--ui-text-muted));
    }

    > .form-control:focus ~ label,
    > .form-select:focus ~ label {
      color: var(--form-tone);
    }
  }
}

/// Emits every form control variant for a single tone.
///
/// @param {String} $name - Class suffix, e.g. `pink`.
/// @param {String} $tone - Token suffix for `--ui-*`.
/// @param {String} $on - Token suffix for `--ui-on-*`.
/// @param {String|null} $mark - Hex colour of the checkmark. `null` keeps the
///   Bootstrap default (white).
@mixin form-tone-plugin($name, $tone: $name, $on: $tone, $mark: null) {
  @include form-control-tone($name, $tone, $on);
  @include form-control-color-tone($name, $tone, $on);
  @include form-file-tone($name, $tone, $on);
  @include form-select-tone($name, $tone, $on);
  @include form-check-tone($name, $tone, $on, $mark);
  @include form-range-tone($name, $tone, $on);
  @include form-input-group-tone($name, $tone, $on);
  @include form-floating-tone($name, $tone, $on);
}
