@use 'sass:map';

@use 'mixins/mixins' as *;
@use 'mixins/utils' as *;
@use 'mixins/var' as *;
@use 'common/var' as *;

@mixin reset-margin {
  @include when(first) {
    margin-left: 0;
    margin-right: 16px;
  }
}

@include b(page) {
  @include set-component-css-var('page', $page);

  white-space: nowrap;
  color: var(--sg-page-text-color);
  font-weight: normal;
  display: flex;
  align-items: center;
  @include utils-clearfix;

  span:not([class*='suffix']),
  button {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: var(--sg-page-font-size);
    min-width: var(--sg-page-button-width);
    height: var(--sg-page-button-height);
    line-height: var(--sg-page-button-height);
    border-radius: var(--sg-page-border-radius);
    box-sizing: border-box;
  }

  .#{$namespace}-input__inner {
    text-align: center;
    -moz-appearance: textfield;
    line-height: normal;
  }

  .#{$namespace}-select .#{$namespace}-input {
    width: 128px;
  }

  button {
    border: none;
    background: transparent;

    &:focus {
      outline: none;
    }

    &:hover {
      color: var(--sg-page-hover-color);
    }

    &:disabled {
      color: var(--sg-page-button-disabled-color);
      background-color: var(--sg-page-button-disabled-bg-color);
      cursor: not-allowed;
    }
  }

  .btn-prev,
  .btn-next {
    background: center center no-repeat;
    background-size: 16px;
    background-color: var(--sg-page-bg-color);
    cursor: pointer;
    margin: 0;
    color: var(--sg-page-button-color);

    .#{$namespace}-icon {
      display: block;
      font-size: 12px;
      font-weight: bold;
      width: inherit;
    }
  }

  .btn-prev {
    margin-right: 8px;
  }

  .btn-next {
    margin-left: 8px;
  }

  .btn-wrap {
    display: flex
  }

  .#{$namespace}-pager li.disabled {
    color: var(--sg-text-color-placeholder);
    cursor: not-allowed;
  }

  @include m(small) {
    .btn-prev,
    .btn-next,
    .#{$namespace}-pager li,
    .#{$namespace}-pager li.btn-quicknext,
    .#{$namespace}-pager li.btn-quickprev,
    .#{$namespace}-pager li:last-child {
      font-size: var(--sg-font-size-extra-small);
      line-height: var(--sg-page-line-height-extra-small);
      height: var(--sg-page-height-extra-small);
      min-width: 24px;
    }

    .arrow.disabled {
      visibility: hidden;
    }

    .more::before,
    li.more::before {
      line-height: var(--sg-page-line-height-extra-small);
    }

    span:not([class*='suffix']),
    button {
      height: var(--sg-page-height-extra-small);
      line-height: var(--sg-page-line-height-extra-small);
      font-size: var(--sg-font-size-extra-small);
    }

    @include e(editor) {
      height: var(--sg-page-line-height-extra-small);
      &.#{$namespace}-input .#{$namespace}-input__inner {
        height: var(--sg-page-height-extra-small);
      }
    }

    .#{$namespace}-input__inner,
    .#{$namespace}-input--small {
      height: var(--sg-page-height-extra-small) !important;
      line-height: var(--sg-page-line-height-extra-small);
    }

    .#{$namespace}-input__suffix {
      line-height: var(--sg-page-line-height-extra-small);
      .#{$namespace}-input__suffix-inner {
        line-height: var(--sg-page-line-height-extra-small);
        i.sg-select__caret {
          line-height: var(--sg-page-line-height-extra-small);
        }
      }
    }

    .#{$namespace}-select .#{$namespace}-input {
      width: 100px;
    }
  }

  @include e(sizes) {
    margin-left: 16px;
    font-weight: normal;
    color: var(--sg-text-color-regular);
    @include reset-margin();
  }

  @include e(total) {
    margin-left: 16px;
    font-weight: normal;
    color: var(--sg-text-color-regular);
    @include reset-margin();

    &[disabled='true'] {
      color: var(--sg-text-color-placeholder);
    }

  }

  @include e(jump) {
    margin-left: 16px;
    font-weight: normal;
    color: var(--sg-text-color-regular);
    @include reset-margin();

    .#{$namespace}-input__inner {
      padding: 0 3px;
    }

    &[disabled='true'] {
      color: var(--sg-text-color-placeholder);
    }
  }

  @include e(editor) {
    line-height: 18px;
    margin: 0 8px;
    height: var(--sg-page-button-height);
    min-width: 56px;

    text-align: center;
    box-sizing: border-box;
    border-radius: var(--sg-page-border-radius);

    &.#{$namespace}-input {
      width: 50px;
    }

    &.#{$namespace}-input .#{$namespace}-input__inner {
      height: var(--sg-page-button-height);
    }

    .#{$namespace}-input__inner::-webkit-inner-spin-button,
    .#{$namespace}-input__inner::-webkit-outer-spin-button {
      -webkit-appearance: none;
      margin: 0;
    }
  }

  @include e(pages) {
    margin-left: 16px;
    font-weight: normal;
    color: var(--sg-text-color-regular);
    @include reset-margin();

    &[disabled='true'] {
      color: var(--sg-text-color-placeholder);
    }
  }

  @include e(holder) {
    flex: 1;
    min-width: 16px;
  }

  @include m(gray) {
    --sg-page-active-color: var(--sg-color-primary);

    .btn-prev,
    .btn-next,
    .#{$namespace}-pager li {
      background-color: map.get($colors, 'info', 'light-9');
      color: var(--sg-text-color-regular);
      border: 0;

      &.disabled {
        color: var(--sg-text-color-placeholder);
        background-color: var(--sg-disabled-bg-color);
      }
    }

    .btn-prev,
    .btn-next {
      &:disabled {
        color: var(--sg-text-color-placeholder);
        background-color: var(--sg-disabled-bg-color);
      }
      &:hover:not([disabled]) {
        color: var(--sg-page-active-color);
      }
    }

    .#{$namespace}-pager li:not(.disabled) {
      &:hover {
        color: var(--sg-page-active-color);
      }

      @include when(active) {
        background-color: var(--sg-color-primary);
        color: var(--sg-color-white);
        border: 0;
        font-weight: bold;
      }
    }
  }

  @include m(block) {
    --sg-page-active-color: var(--sg-color-primary);

    .btn-prev,
    .btn-next,
    .#{$namespace}-pager li {
      color: var(--sg-text-color-regular);
      border: 1px solid var(--sg-page-border-color);

      &.disabled {
        color: var(--sg-text-color-placeholder);
        background-color: var(--sg-disabled-bg-color);
      }
    }

    .btn-prev,
    .btn-next {
      &:disabled {
        color: var(--sg-text-color-placeholder);
        background-color: var(--sg-disabled-bg-color);
      }
      &:hover:not([disabled]) {
        color: var(--sg-page-active-color);
        border: 1px solid var(--sg-page-active-color);
      }
    }

    .#{$namespace}-pager li:not(.disabled) {
      &:hover {
        color: var(--sg-page-active-color);
        border-color: var(--sg-page-active-color);
      }

      @include when(active) {
        background-color: var(--sg-color-primary);
        color: var(--sg-color-white);
        border-color: var(--sg-color-primary);
        font-weight: bold;
      }
    }
  }

  @each $position, $value in (left: start, right: end) {
    @include m($position) {
      justify-content: $value;
    }
  }
}

@include b(pager) {
  user-select: none;
  list-style: none;
  font-size: 0;
  padding: 0;
  margin: 0;
  display: flex;
  align-items: center;

  li {
    background: var(--sg-page-bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: var(--sg-page-font-size);
    min-width: var(--sg-page-button-width);
    height: var(--sg-page-button-height);
    line-height: var(--sg-page-button-height);
    border-radius: var(--sg-page-border-radius);
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;

    &.btn-quickprev:hover {
      cursor: pointer;
    }

    &.btn-quicknext:hover {
      cursor: pointer;
    }

    &.btn-quicknext,
    &.btn-quickprev {
      line-height: 32px;
      color: var(--sg-page-button-color);

      &.disabled {
        color: var(--sg-text-color-placeholder);
        cursor: not-allowed;
      }

      svg {
        pointer-events: none;
      }
    }

    &:focus-visible {
      outline: 1px solid var(--sg-page-hover-color);
    }

    &:hover {
      color: var(--sg-page-hover-color);
    }

    @include when(active) {
      color: var(--sg-page-hover-color);
      border: 1px solid var(--sg-page-hover-color);
      cursor: default;
    }

    &:not(:last-of-type) {
      margin-right: 8px;
    }
  }

  & + button.btn-next[type='button'] {
    margin-right: 0;
  }
}
