// This file is part of Indico.
// Copyright (C) 2002 - 2026 CERN
//
// Indico is free software; you can redistribute it and/or
// modify it under the terms of the MIT License; see the
// LICENSE file for more details.

@use 'base/palette' as *;
@use 'base/utilities' as *;
@use 'base/defaults' as *;
@use './buttons' as buttons;

$toolbar-height: 2.2em;
$toolbar-thin-height: 1.9em;
$toolbar-spacing: 10px;

@mixin toolbar-group {
  @include toolbar-group-inputs();
  @include toolbar-group-datepicker();
  @include toolbar-group-slider();
  @extend .flexrow;

  & ~ .i-button {
    margin-left: buttons.$i-button-spacing;
  }

  .i-button {
    border-radius: 0;
    border-right-width: 0;
    margin: 0;

    // i-button flavors don't change border color on hover
    &:not(.label):not(.accept):not(.danger):not(.highlight):not(.warning):not(.disabled):not(:disabled) {
      &:hover + .i-button {
        border-left-color: $gray;
      }
    }
  }

  ind-menu > .i-button {
    border-radius: 0 !important;
    border-right-width: 0 !important;
    margin: 0;
  }

  .i-button.arrow:last-of-type {
    border-bottom-right-radius: $default-border-radius;
    border-top-right-radius: $default-border-radius;
  }

  & > .i-button:not(.hidden) {
    &:first-child {
      border-bottom-left-radius: $default-border-radius;
      border-top-left-radius: $default-border-radius;
    }

    &:last-child {
      border-bottom-right-radius: $default-border-radius;
      border-top-right-radius: $default-border-radius;
      border-right-width: 1px;
    }

    &.label {
      &:not(:last-child) {
        border-bottom-right-radius: 0;
        border-top-right-radius: 0;
        border-right: none !important;
      }

      & ~ .i-button:not(.hidden):first-child {
        border-bottom-left-radius: 0;
        border-top-left-radius: 0;
      }
    }
  }

  & > ind-with-tooltip:first-child ind-menu > .i-button,
  & > ind-menu:first-child > .i-button {
    border-bottom-left-radius: $default-border-radius;
    border-top-left-radius: $default-border-radius;
  }

  & > ind-with-tooltip:last-child ind-menu > .i-button,
  & > ind-menu:last-child > .i-button {
    border-bottom-right-radius: $default-border-radius;
    border-top-right-radius: $default-border-radius;
    border-right-width: 1px;
  }

  .i-button-wrapper:not(.hidden) {
    &:first-child .i-button {
      border-bottom-left-radius: $default-border-radius;
      border-top-left-radius: $default-border-radius;
    }

    &:last-child .i-button {
      border-bottom-right-radius: $default-border-radius;
      border-top-right-radius: $default-border-radius;
      border-right-width: 1px;
    }
  }
}

@mixin toolbar-group-inputs {
  label.i-button {
    &:not(.label) {
      &:hover ~ .i-button {
        border-left-color: $gray;
      }

      &:hover ~ .i-button ~ .i-button {
        border-left-color: $default-border-color;
      }
    }
  }

  input[type='radio']:first-child,
  input[type='text']:first-child,
  input[type='password']:first-child {
    + .i-button {
      border-bottom-left-radius: $default-border-radius;
      border-top-left-radius: $default-border-radius;
    }
  }

  > .clearableinput {
    float: left;
  }

  > input[type='text']:first-child,
  > input[type='password']:first-child,
  > .clearableinput:first-child input[type='text'] {
    border-bottom-right-radius: $default-border-radius;
    border-top-right-radius: $default-border-radius;
  }

  > input[type='text']:last-child,
  > input[type='password']:last-child,
  > .clearableinput:last-child input[type='text'] {
    border-bottom-left-radius: 0;
    border-top-left-radius: 0;
  }

  > input[type='text']:not(:last-child),
  > input[type='password']:not(:last-child),
  > .clearableinput:not(:last-child) {
    border-bottom-right-radius: 0;
    border-top-right-radius: 0;

    & + .i-button {
      border-left: none;
      border-bottom-left-radius: 0;
      border-top-left-radius: 0;
    }
  }
}

@mixin toolbar-group-slider {
  &.with-slider {
    .slider {
      padding-left: 1em !important;
      padding-right: 1em !important;
      width: 150px;

      .ui-slider {
        display: inline-block !important;
        margin-top: 3px;
        margin-left: -2px;
        width: 100% !important;
      }
    }
  }
}

@mixin toolbar-group-datepicker {
  &.with-datepicker {
    .datepicker {
      position: relative;

      &:last-child {
        input[type='text'] {
          border-bottom-right-radius: $default-border-radius;
          border-top-right-radius: $default-border-radius;
        }
      }

      .ui-datepicker-trigger {
        float: right;
        font-size: 1em;
        position: absolute;
        margin: 0;
        right: 5px;
        top: auto;
      }

      &.thin .ui-datepicker-trigger {
        line-height: $toolbar-thin-height;
      }
    }
  }
}

.sticky-scrolling .toolbar {
  margin: 0.5em 0 0.5em 0;
}

.toolbar {
  $toolbar-button-height: 1.7em;

  display: flex;
  flex-shrink: 0;
  align-items: center;
  gap: 0.5em;
  min-height: $toolbar-height;

  &.fixed-height {
    max-height: 1.75rem;
  }

  &.space-before {
    margin-top: $toolbar-spacing;
  }

  &.space-after {
    margin-bottom: $toolbar-spacing;
  }

  &.thin {
    $thin-button-height: 1.4em;
    min-height: $toolbar-thin-height;

    & .i-button {
      height: $toolbar-thin-height;
    }

    & input[type='text'] {
      height: $toolbar-thin-height;
    }

    :not(.ui) > input[type='text'] {
      height: $toolbar-thin-height;
    }
  }

  &.thinner {
    min-height: 1em;

    & .i-button {
      height: 1.5em;
    }

    & .i-button[class*='icon-']::before {
      font-size: 1em;
    }
  }

  & > .option {
    line-height: 2.5em;
    margin-right: 1em;
  }

  .i-button {
    height: $toolbar-height;
    padding: 0.2em 0.7em 0.1em 0.7em !important;

    &:not(.label) {
      font-weight: bold;
    }

    &.arrow:last-of-type:not(.borderless) {
      border-right-width: 1px;
      border-right-style: solid;
    }
  }

  :not(.ui) > input[type='text'] {
    color: $black;
    display: inline-block;
    float: left;
    height: $toolbar-height;
    margin: 0;
    padding: 0.2em 0.7em 0.1em 0.7em;
  }

  .i-dropdown {
    z-index: 3;

    & > li {
      white-space: nowrap;
    }
  }

  .group {
    @include toolbar-group();
  }

  .search-box {
    min-width: 300px;
  }
}

.toolbars {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: -$toolbar-spacing;

  &.space-before {
    margin-top: 0;
  }

  &.space-after {
    margin-bottom: $toolbar-spacing;
  }

  .toolbar {
    margin-top: $toolbar-spacing;

    &:not(:last-child) {
      margin-right: 0.5em;
    }
  }
}

.toolbar.table {
  display: table;
  table-layout: fixed;

  .group {
    display: table-row;

    .i-button {
      @include ellipsis();

      display: table-cell;
      float: none;
    }
  }
}
