/*
 * 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-text-helpers';
@use '../../scss-shared/nx-container-helpers';
@use '../../scss-shared/nx-button-helpers';

$-triangle-box-width: var(--nx-spacing-6x);
$-trigger-side-padding: var(--nx-spacing-2x);
$-trigger-text-left-margin: var(--nx-spacing-base);
$-form-child-left-padding: calc(#{$-trigger-side-padding} + #{$-triangle-box-width} + #{$-trigger-text-left-margin});

/*
  #Tree view
*/
.nx-collapsible-items {
  box-sizing: border-box;
  margin: var(--nx-spacing-6x) calc(0px - var(--nx-spacing-2x));

  // Design specifies 400px max-width plus 16px of l/r negative margin
  max-width: 400px;

  + .nx-collapsible-items {
    // adjacent treeviews should be 16px apart rather than the 24 that they are from everything else
    margin-top: calc(0px - var(--nx-spacing-2x));
  }

  .nx-text-input {
    box-sizing: border-box;
    border: none;
    margin: 0;
    padding:
      var(--nx-spacing-3x)
      var(--nx-spacing-2x)
      var(--nx-spacing-3x)
      $-form-child-left-padding;

    width: 100%;
  }
}

.nx-collapsible-items--expanded > .nx-collapsible-items__header {
  > .nx-collapsible-items__trigger, > .nx-collapsible-items__tooltip-wrapper > .nx-collapsible-items__trigger {
    > .nx-collapsible-items__twisty {
      transform: rotate(90deg);
    }
  }
}

.nx-collapsible-items--collapsed {
  .nx-collapsible-items__children, .nx-filter-input {
    display: none;
  }
}

.nx-collapsible-items--disabled {

  .nx-collapsible-items__trigger {
    cursor: default;
    color: var(--nx-color-text-disabled);
  }
}

.nx-collapsible-items--empty {
  .nx-collapsible-items__trigger {
    cursor: default;
  }

  .nx-collapsible-items__twisty {
    visibility: hidden;
  }
}

.nx-collapsible-items__header,
.nx-collapsible-items__tooltip-wrapper {
  display: flex;
  gap: var(--nx-spacing-6x);
}

.nx-collapsible-items__tooltip-wrapper {
  flex-grow: 1;
}

.nx-collapsible-items__action-content {
  .nx-btn--icon-only {
    @include nx-button-helpers.small-icon-btn;
  }

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

%collapsible-items-focus-style {
  outline: var(--nx-focus-outline-width) solid var(--nx-color-interactive-border-focus);
  outline-offset: calc(0px - var(--nx-focus-outline-offset));
}

.nx-collapsible-items__trigger {
  align-items: center;
  background: none;
  border: 0;
  border-radius: var(--nx-border-radius);
  box-sizing: border-box;
  color: var(--nx-color-text-stark);
  cursor: pointer;
  display: grid;
  font: inherit;
  font-weight: 600;

  grid-template-columns: $-triangle-box-width 1fr;
  text-align: left;
  padding: var(--nx-spacing-base) $-trigger-side-padding;
  width: 100%;

  &:hover:not([disabled]) {
    background-color: var(--nx-color-interactive-background-hover);
  }

  &:focus {
    @extend %collapsible-items-focus-style;
  }

  &:active:not([disabled]),
  &:active:not([disabled]):focus {
    background-color: var(--nx-color-interactive-background-active);
    outline: none;
  }

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

.nx-collapsible-items__twisty {
  align-self: center;
  font-size: 22px;
  justify-self: center;
  margin: 0;
  transition: transform 300ms ease;

  &.nx-collapsible-items--empty {
    visibility: hidden;
  }
}

.nx-collapsible-items__text {
  @include nx-text-helpers.truncate-ellipsis;

  align-items: center;
  display: flex;
  margin-left: $-trigger-text-left-margin;

  span {
    @include nx-text-helpers.truncate-ellipsis;
    flex-grow: 1;
  }

  > .nx-counter {
    @include nx-text-helpers.truncate-ellipsis;
    box-sizing: border-box;
    flex-shrink: 0;
    max-width: 110px;
  }

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

.nx-collapsible-items__child {
  border: 0;
  border-radius: var(--nx-border-radius);
  box-sizing: border-box;
  color: var(--nx-color-text-stark);
  display: block;
  overflow: hidden;

  padding:
    var(--nx-spacing-base)
    var(--nx-spacing-6x)
    var(--nx-spacing-base)
    calc(#{$-trigger-side-padding} + #{$-triangle-box-width} + var(--nx-spacing-3x));

  text-overflow: ellipsis;
  white-space: nowrap;

  &.nx-radio-checkbox {
    box-sizing: border-box;
    border: none;
    display: flex;
    margin: 0;
    padding:
      var(--nx-spacing-base)
      var(--nx-spacing-2x)
      var(--nx-spacing-base)
      $-form-child-left-padding;
  }
}

button, a {
  &.nx-collapsible-items__child,
  &.nx-btn.nx-collapsible-items__child {
    background-color: transparent;
    font-weight: normal;
    margin: 0;
    text-align: left;
    text-decoration: none;
    width: 100%;

    &:hover {
      background-color: var(--nx-color-interactive-background-hover);
      color: var(--nx-color-text-stark);
    }

    &.selected {
      background-color: var(--nx-color-interactive-background-selected);
      font-weight: 600;
    }

    &:focus {
      @extend %collapsible-items-focus-style;
    }

    &:active {
      color: var(--nx-color-text-stark);
    }

    &:active:focus {
      outline: none;
    }
  }
}

// sidebars don't typically have the white background and so warrant different highlight colors
.nx-page-sidebar {
  button, a {
    &.nx-collapsible-items__child {
      &:hover {
        background-color: var(--nx-color-collapsible-items-sidebar-background-hover);
      }

      &.selected {
        background-color: var(--nx-color-interactive-background-selected);
      }
    }
  }

  .nx-collapsible-items__trigger {
    &:hover:not([disabled]) {
      background-color: var(--nx-color-collapsible-items-sidebar-background-hover);
    }

    &:active:not([disabled]),
    &:active:not([disabled]):focus {
      background-color: var(--nx-color-collapsible-items-sidebar-background-active);
      outline: none;
    }
  }
}
