@use "../../foundations/helpers/forward.helpers.scss" as *;
@use "../../assets/form-field/form-field.scss" as *;
@use "../icon-sprite/icon-sprite.scss";

$ds-checkbox__icon-size: 24px;

.ds-checkbox {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;

  .ds-checkbox__inner {
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: $ds-color-text-primary;
    position: relative;
    padding: ds-spacing($ds-s-025);

    &::before {
      content: "";
      position: absolute;
      border-radius: ds-border-radius(012);
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
    }

    &:hover {
      .ds-checkbox__text {
        text-decoration: underline;
      }
    }

    @include min-click-surface();

    .ds-checkbox__text {
      margin: ds-spacing(0 0 0 $ds-s-050, rem);
      @include ds-typography($ds-typography-functional-body-md);

      @at-root .ds-force-px#{&} {
        margin: ds-spacing(0 0 0 $ds-s-050);
        @include ds-typography($ds-typography-functional-body-md, true);
      }
    }

    .ds-checkbox__icon {
      line-height: 0;
      display: block;
      position: relative;
      height: ds-px-to-rem($ds-checkbox__icon-size);
      width: ds-px-to-rem($ds-checkbox__icon-size);
      color: $ds-color-text-primary;
      @at-root .ds-force-px#{&} {
        height: $ds-checkbox__icon-size;
        width: $ds-checkbox__icon-size;
      }
    }
    .ds-icon {
      height: 100%;
      width: 100%;
      position: absolute;
      top: 0;
      left: 0;
      svg {
        fill: currentColor;
      }
      &.ds-icon--check_box-filled {
        display: none;
      }
    }
  }

  input {
    opacity: 0;
    position: absolute;

    &:checked + .ds-checkbox__inner {
      .ds-icon--check_box-filled {
        display: block;
      }
      .ds-icon--check_box_outline_blank {
        display: none;
      }
    }

    &:focus-visible + .ds-checkbox__inner {
      border-radius: ds-border-radius(012);
      outline: ds-border-width(xs) solid $ds-color-border-primary-strongest;
      outline-offset: 2px;
    }
  }

  &.ds-form-field__error,
  .invalid {
    .ds-checkbox__inner .ds-checkbox__icon {
      color: $ds-color-text-critical;
    }

    .ds-form-field__error-message {
      margin-top: ds-spacing($ds-s-025);
      padding: ds-spacing(0 $ds-s-025 $ds-s-025);

      .ds-icon {
        margin: ds-spacing(0 $ds-s-050 0 $ds-s-200, rem);
        @at-root .ds-force-px#{&} {
          margin: ds-spacing(0 $ds-s-050 0 $ds-s-200);
        }
      }
    }
  }
}

.ds-checkbox input:disabled + .ds-checkbox__inner {
  cursor: not-allowed;
  .ds-checkbox__icon {
    color: $ds-color-text-primary-disabled;
  }
  .ds-checkbox__text {
    color: $ds-color-text-primary-disabled;
  }
  &::before {
    background-color: unset;
  }
}
