/*
 * Copyright (c) 2019-present Sonatype, Inc.
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which accompanies this
 * distribution and is available at https://www.eclipse.org/legal/epl-2.0/.
 */
@use '../scss-shared/nx-container-helpers';
@use '../scss-shared/nx-text-helpers';

.nx-form {
  position: relative;

  &__required-field-asterisk {
    color: var(--nx-color-validation-invalid);
  }

  &__required-field-notice {
    font-weight: 600;
    color: var(--nx-color-text-stark);
  }

  &__validation-errors {
    display: none;

    + .nx-btn-bar {
      margin-top: 0;
    }
  }

  &--show-validation-errors {
    &.nx-form--has-validation-errors {
      .nx-form__validation-errors {
        display: flex;
      }
    }
  }

  &--has-submit-error {
    .nx-form__validation-errors {
      display: none;
    }
  }

  &--has-submit-error, &--show-validation-errors.nx-form--has-validation-errors {
    .nx-form__submit-btn {
      display: none;
    }

    // remove right margin from the last _visible_ footer button
    .nx-footer .nx-btn-bar > :nth-last-child(2) {
      margin-right: 0;
    }
  }

  @include nx-container-helpers.container-vertical;
}


// Basic form layout
.nx-form-group {
  margin-bottom: var(--nx-spacing-8x);

  .nx-text-input, .nx-form-select, .nx-file-upload, .nx-multi-file-upload {
    margin-bottom: 0;
  }
}

.nx-form-row {
  align-items: flex-end;
  display: flex;
  margin-bottom: var(--nx-spacing-8x);

  .nx-form-group {
    margin-right: var(--nx-spacing-6x);
    margin-bottom: 0;
  }

  .nx-btn-bar {
    margin-top: 0;
    margin-bottom: 0;
  }

  @include nx-container-helpers.container-horizontal;
}

// labels

.nx-label, .nx-legend {
  display: block;
  padding: 0;
  max-width: var(--nx-width-text-wrapping);

  .nx-text-input, .nx-textarea {
    display: block;
  }
}

// hack to get Safari to recognize the legend's text margins
.nx-legend {
  -webkit-margin-collapse: separate;
}

.nx-label__text, .nx-legend__text {
  @include nx-text-helpers.bold;

  color: var(--nx-color-text-stark);
  cursor: default;
  display: block;
  margin-bottom: var(--nx-spacing-2x);

  &::after {
    @extend .nx-form__required-field-asterisk;
    content: ' *';
  }

  @include nx-container-helpers.container-horizontal;
}

.nx-label--optional, .nx-legend--optional {
  .nx-label__text::after, .nx-legend__text::after {
    display: none;
  }
}

.nx-sub-label {
  display: block;
  font-size: var(--nx-font-size-s);
  font-style: italic;
  margin-bottom: var(--nx-spacing-4x);
  max-width: var(--nx-width-text-wrapping);

  @include nx-container-helpers.container-horizontal;
}
