@use '@material/textfield' as mdc-textfield;
@use '@material/theme/variables' as mdc-theme-variables;
@use 'sass:color';

// Mixin that refreshes the MDC text-field theming variables. This mixin should be used when
// the base MDC theming variables have been explicitly updated, but the component specific
// theming-based variables are still based on the old MDC base theming variables. The mixin
// restores the previous values for the variables to avoid unexpected global side effects.
@mixin private-text-field-refresh-theme-variables() {
  $_disabled-border: mdc-textfield.$disabled-border;
  mdc-textfield.$disabled-border: rgba(mdc-theme-variables.prop-value(on-surface), 0.06);
  $_bottom-line-hover: mdc-textfield.$bottom-line-hover;
  mdc-textfield.$bottom-line-hover: rgba(mdc-theme-variables.prop-value(on-surface), 0.87);
  $_bottom-line-idle: mdc-textfield.$bottom-line-idle;
  mdc-textfield.$bottom-line-idle: rgba(mdc-theme-variables.prop-value(on-surface), 0.42);
  $_label: mdc-textfield.$label;
  mdc-textfield.$label: rgba(mdc-theme-variables.prop-value(on-surface), 0.6);
  $_ink-color: mdc-textfield.$ink-color;
  mdc-textfield.$ink-color: rgba(mdc-theme-variables.prop-value(on-surface), 0.87);
  $_focused-label-color: mdc-textfield.$focused-label-color;
  mdc-textfield.$focused-label-color: rgba(mdc-theme-variables.prop-value(primary), 0.87);
  $_placeholder-ink-color: mdc-textfield.$placeholder-ink-color;
  mdc-textfield.$placeholder-ink-color: rgba(mdc-theme-variables.prop-value(on-surface), 0.6);
  $_disabled-label-color: mdc-textfield.$disabled-label-color;
  mdc-textfield.$disabled-label-color: rgba(mdc-theme-variables.prop-value(on-surface), 0.38);
  $_disabled-ink-color: mdc-textfield.$disabled-ink-color;
  mdc-textfield.$disabled-ink-color: rgba(mdc-theme-variables.prop-value(on-surface), 0.38);
  $_disabled-placeholder-ink-color: mdc-textfield.$disabled-placeholder-ink-color;
  mdc-textfield.$disabled-placeholder-ink-color:
      rgba(mdc-theme-variables.prop-value(on-surface), 0.38);
  $_background: mdc-textfield.$background;
  mdc-textfield.$background: color.mix(mdc-theme-variables.prop-value(on-surface),
    mdc-theme-variables.prop-value(surface), 4%);
  $_disabled-background: mdc-textfield.$disabled-background;
  mdc-textfield.$disabled-background: color.mix(mdc-theme-variables.prop-value(on-surface),
    mdc-theme-variables.prop-value(surface), 2%);
  $_outlined-idle-border: mdc-textfield.$outlined-idle-border;
  mdc-textfield.$outlined-idle-border: rgba(mdc-theme-variables.prop-value(on-surface), 0.38);
  $_outlined-disabled-border: mdc-textfield.$outlined-disabled-border;
  mdc-textfield.$outlined-disabled-border: rgba(mdc-theme-variables.prop-value(on-surface), 0.06);
  $_outlined-hover-border: mdc-textfield.$outlined-hover-border;
  mdc-textfield.$outlined-hover-border: rgba(mdc-theme-variables.prop-value(on-surface), 0.87);

  // The content will be generated with the refreshed MDC text-field theming variables.
  @content;

  // Reset all variables to ensure that this mixin does not cause unexpected side effects.
  mdc-textfield.$disabled-border: $_disabled-border;
  mdc-textfield.$bottom-line-hover: $_bottom-line-hover;
  mdc-textfield.$bottom-line-idle: $_bottom-line-idle;
  mdc-textfield.$label: $_label;
  mdc-textfield.$ink-color: $_ink-color;
  mdc-textfield.$focused-label-color: $_focused-label-color;
  mdc-textfield.$placeholder-ink-color: $_placeholder-ink-color;
  mdc-textfield.$disabled-label-color: $_disabled-label-color;
  mdc-textfield.$disabled-ink-color: $_disabled-ink-color;
  mdc-textfield.$disabled-placeholder-ink-color: $_disabled-placeholder-ink-color;
  mdc-textfield.$background: $_background;
  mdc-textfield.$disabled-background: $_disabled-background;
  mdc-textfield.$outlined-idle-border: $_outlined-idle-border;
  mdc-textfield.$outlined-disabled-border: $_outlined-disabled-border;
  mdc-textfield.$outlined-hover-border: $_outlined-hover-border;
}
