@use '../../00-settings/settings' as *;
@use '../../00-mixins/mixins' as *;
@use '../../11-base/fonts/icons' as *;

/* stylelint-disable no-descending-specificity */

////
/// This file defines general form input fields styling.
///
/// @group forms
/// @author Gert-Jan Meire
///
////

$field-icon-position: no-repeat right 15px top 8px;
$field-icon-padding: 50px;

///
/// The general field atom styling.
///
/// @since 3.0.0
/// @group theming
/// @access public
/// @author Gert-Jan Meire
/// @require color
/// @require svg-icon
/// @require svg-url
///
@mixin field-base {
  @include theme("border-color", "color-zero-medium", "fields-border-color");
  @include theme("background", "color-none", "fields-background");
  @include theme("color", "color-zero", "fields-color");

  &:focus {
    @include theme("border-color", "color-zero", "fields-border-focus-color");
    @include theme("background", "color-none", "fields-focus-background");

    border: 1px solid;
  }

  &:disabled {
    padding-right: $field-icon-padding;
    border: 0;
    background: coloring("gray-light") $field-icon-position;
    background-image: svg-as-background(
      "lock-closed",
      coloring("gray", -2),
      24,
      24
    );
    cursor: not-allowed;
  }

  &.success {
    padding-right: $field-icon-padding;
    border: 1px solid coloring("green");
    background: coloring("green-light") $field-icon-position;
    background-image: svg-as-background(
      "checkmark-circle",
      coloring("green"),
      26,
      26
    );

    &:focus {
      border-color: coloring("green");
      background: coloring("green-light") $field-icon-position;
      background-image: svg-as-background(
        "checkmark-circle",
        coloring("green"),
        26,
        26
      );
    }
  }

  &.error {
    padding-right: $field-icon-padding;
    border: 1px solid coloring("red");
    background: coloring("red-light") $field-icon-position;
    background-image: svg-as-background(
      "exclamation-circle",
      coloring("red"),
      26,
      26
    );

    &:focus {
      border-color: coloring("red");
      background: coloring("red-light") $field-icon-position;
      background-image: svg-as-background(
        "exclamation-circle",
        coloring("red"),
        26,
        26
      );
    }
  }

  &::-webkit-input-placeholder { // stylelint-disable-line selector-no-vendor-prefix
    @include theme("color", "color-zero", "fields-placeholder-color");

    font-weight: normal;
  }

  &:-moz-placeholder { // stylelint-disable-line selector-no-vendor-prefix
    @include theme("color", "color-zero", "fields-placeholder-color");

    font-weight: normal;
  }

  &::-moz-placeholder { // stylelint-disable-line selector-no-vendor-prefix
    @include theme("color", "color-zero", "fields-placeholder-color");

    font-weight: normal;
  }

  &:-ms-input-placeholder { // stylelint-disable-line selector-no-vendor-prefix
    @include theme("color", "color-zero", "fields-placeholder-color");

    font-weight: normal;
  }

  &::placeholder {
    @include theme("color", "color-zero", "fields-placeholder-color");

    font-weight: normal;
  }

  display: block;
  width: 100%;
  height: 44px;
  padding: 0 16px;
  border: 1px solid;
  font-family: $default-font-family;
  font-size: .8rem;
  font-weight: normal;
  appearance: none;
}

///
/// The base styling for choice fields.
///   - Radio buttons.
///   - Checkboxes.
///
/// @since 3.0.0
/// @group theming
/// @access public
/// @author Gert-Jan Meire
///
@mixin choices-field-base($type) {
  position: relative;
  margin-bottom: 10px;

  &:last-child {
    margin-bottom: 0;
  }

  label {
    position: absolute;
    top: 0;
    left: 0;
    padding-left: 28px;
    overflow: unset;
    font-weight: normal;
    line-height: 28px;

    &::before,
    &::after {
      content: "";
      position: absolute;
      top: 3px;
    }

    &::before {
      display: inline-block;
      left: 0;
      width: 20px;
      height: 20px;
      border: 2px solid;
    }

    &::after {
      display: block;
    }
  }

  @if $type == "checkbox" {
    label {
      &::before {
        @include theme(
          "background-color",
          "color-none",
          "checkbox-background-color"
        );
        @include theme(
          "border-color",
          "color-zero-medium",
          "checkbox-border-color"
        );
      }

      &::after {
        top: 3px;
        left: -2px;
        width: 20px;
        height: 20px;
      }
    }
  }

  @if $type == "radio" {
    label {
      &::before {
        @include theme(
          "background-color",
          "color-none",
          "radio-background-color"
        );
        @include theme(
          "border-color",
          "color-zero-medium",
          "radio-border-color"
        );

        border-radius: border-radius("radius-4");
      }

      &::after {
        top: 9px;
        left: 6px;
        width: 8px;
        height: 8px;
        border-radius: border-radius("radius-4");
      }
    }
  }

  input[type="radio"],
  input[type="checkbox"] {
    position: absolute;
    opacity: 0;

    //
    // Styling of the label.
    //
    + label {
      @include theme("color", "color-zero", "label-color");

      position: relative;

      // Hide checkmark by default.
      &::after {
        content: none;
      }
    }

    //
    // Checkbox specific success state.
    // Checkbox specific error state.
    //
    [class*="cs--"] & {
      &.success + label::before {
        border-color: coloring("green");
      }

      &.error + label::before {
        border-color: coloring("red");
      }
    }

    //
    // Disabled state.
    //
    &:disabled {
      + label {
        color: coloring('gray', -3);
        font-weight: normal !important; // stylelint-disable-line declaration-no-important
        cursor: not-allowed;

        &::before {
          border-color: coloring(
            "gray",
            -5
          ) !important; // stylelint-disable-line declaration-no-important

          background: coloring(
            "gray",
            -5
          ) !important; // stylelint-disable-line declaration-no-important
        }

        &::after {
          color: coloring(
            "gray",
            -2
          ) !important; // stylelint-disable-line declaration-no-important

        }
      }
    }

    //
    // Checkbox specific styling.
    //
    @if $type == "checkbox" {
      // Checked state.
      &:checked {
        // Show checkmark on checked.
        + label {
          @include icon("checkmark-checkbox", "after");

          &::after {
            @include theme("color", "color-primary", "checkbox-checkmark-color");

            padding-left: 1px;
            font-size: 1.1rem;
            line-height: 1;
          }
        }

        + label::before {
          @include theme(
            "background-color",
            "color-none",
            "checkbox-background-color-checked"
          );
          @include theme(
            "border-color",
            "color-primary",
            "checkbox-border-color-checked"
          );
        }
      }
    }

    //
    // Radio specific styling.
    //
    @if $type == "radio" {
      // Checked state.
      &:checked {
        + label::before {
          @include theme(
            "background-color",
            "color-none",
            "radio-background-color-checked"
          );
          @include theme(
            "border-color",
            "color-primary",
            "radio-border-color-checked"
          );
        }

        + label::after {
          @include theme("background-color", "color-primary", "radio-checkmark-color");

          content: "";
        }

        &:disabled {
          + label {
            &::after {
              background-color: coloring(
                "gray",
                -2
              ) !important; // stylelint-disable-line declaration-no-important
            }
          }
        }
      }
    }
  }
}

.form-disclaimer {
  @include theme('color', 'color-tertiary', 'form-disclaimer-color');

  .highlight__inner &,
  .feature-block &,
  .feature-block.secondary & {
    @include theme('color', 'color-zero');
  }

  margin: 24px 0;
  font-size: .6rem;
}
