/* ------------------------------------ *\
   Button
\* ------------------------------------ */

// Dev Notes
// 1. [Morse] This mixin outputs a separate ruleset for each selector to prevent IE from failing on unrecognized selectors like `:host`. It is not a substitute for comma-separated selectors.
// 2. [Morse] Bolt-link delegates focus to inner element.
// 3. [Mai] These selectors and rules must be separated to override browser defaults.

@import '@bolt/core-v3.x';
@import './button-settings-and-tools';

/**
 * Base Button
 */
@include bolt-repeat-rule(('bolt-button', ':host(bolt-button)')) {
  // [1]
  display: inline-flex;
  outline: none; // [2]
}

@include bolt-repeat-rule(('bolt-button[type]', ':host(bolt-button[type])')) {
  @include bolt-button-native-styles-reset;
}

.c-bolt-button {
  @include bolt-button;
}

/**
 * Button Styles
 */
.c-bolt-button--primary {
  @include bolt-button-style(primary);
}

.c-bolt-button--secondary {
  @include bolt-button-style(secondary);
}

.c-bolt-button--tertiary {
  @include bolt-button-style(tertiary);
}

.c-bolt-button--text {
  @include bolt-button-style(text);
}

[aria-disabled='true'] .c-bolt-button,
.c-bolt-button[disabled],
.c-bolt-button--disabled {
  @include bolt-button-style(disabled);
}

/**
 * Button Text Transform
 */
.c-bolt-button--uppercase {
  text-transform: uppercase;
}

.c-bolt-button--lowercase {
  text-transform: lowercase;
}

.c-bolt-button--capitalize {
  text-transform: capitalize;
}

/**
 * Button Sizes
 */
.c-bolt-button--medium {
  padding: calc(var(--bolt-spacing-y-medium) / 2) var(--bolt-spacing-x-medium);
  font-size: var(--bolt-type-font-size-xsmall);
  line-height: var(--bolt-type-line-height-xsmall);
}

.c-bolt-button--xsmall {
  padding: calc(var(--bolt-spacing-y-xsmall) / 2) var(--bolt-spacing-x-xsmall);
  font-size: var(--bolt-type-font-size-xsmall);
  line-height: var(--bolt-type-line-height-xsmall);
}

.c-bolt-button--small {
  padding: calc(var(--bolt-spacing-y-small) / 2) var(--bolt-spacing-x-small);
  font-size: var(--bolt-type-font-size-xsmall);
  line-height: var(--bolt-type-line-height-xsmall);
}

.c-bolt-button--large {
  padding: calc(var(--bolt-spacing-y-medium) / 2) var(--bolt-spacing-x-medium);
  font-size: var(--bolt-type-font-size-medium);
  line-height: var(--bolt-type-line-height-medium);
}

.c-bolt-button--xlarge {
  padding: calc(var(--bolt-spacing-y-large) / 2) var(--bolt-spacing-x-large);
  font-size: var(--bolt-type-font-size-large);
  line-height: var(--bolt-type-line-height-large);
}

/**
 * Icon Only Buttons
 */
@function bolt-button-icon-only-size($padding_size, $font_size: xsmall) {
  @return calc(
    var(--bolt-type-line-height-#{$font_size}) *
      var(--bolt-type-font-size-#{$font_size}) +
      var(--bolt-spacing-y-#{$padding_size})
  );
}

.c-bolt-button--icon-only {
  padding: 0;

  .c-bolt-button__item,
  .c-bolt-button__icon {
    margin: 0;
  }

  .c-bolt-button__item {
    opacity: 0;
    width: 0;
    overflow: hidden;
    color: transparent;
    text-indent: -99999em;
    white-space: nowrap;
  }

  .c-bolt-button__icon-sizer {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate3d(-50%, -50%, 0);
  }

  // Scale icon only version's icon size based on button size
  &.c-bolt-button--xsmall {
    width: bolt-button-icon-only-size(xsmall);
    min-height: bolt-button-icon-only-size(xsmall);

    .c-bolt-button__icon-sizer {
      font-size: 1em;
    }
  }

  &.c-bolt-button--small {
    width: bolt-button-icon-only-size(small);
    min-height: bolt-button-icon-only-size(small);

    .c-bolt-button__icon-sizer {
      font-size: 1.25em;
    }
  }

  &.c-bolt-button--medium {
    width: bolt-button-icon-only-size(medium);
    min-height: bolt-button-icon-only-size(medium);

    .c-bolt-button__icon-sizer {
      font-size: 1.5em;
    }
  }

  &.c-bolt-button--large {
    width: bolt-button-icon-only-size(medium, medium);
    min-height: bolt-button-icon-only-size(medium, medium);

    .c-bolt-button__icon-sizer {
      font-size: 1.75em;
    }
  }

  &.c-bolt-button--xlarge {
    width: bolt-button-icon-only-size(large, large);
    min-height: bolt-button-icon-only-size(large, large);

    .c-bolt-button__icon-sizer {
      font-size: 2em;
    }
  }
}

/**
 * Button Width
 */
bolt-button[width='full'],
.c-bolt-button--full {
  width: 100%;
}

// :host selector for correctly styling the <bolt-button> element inside other components
// writing this out in a separate selector due to unsupporting browsers throwing out the :host selector
:host([width='full']) {
  width: 100%;
}

// full@small width variations
bolt-button[width='full@small'],
.c-bolt-button--full\@small {
  @include bolt-mq($until: small) {
    width: 100%;
  }
}

// :host selector for correctly styling the <bolt-button> element inside other components
// writing this out in a separate selector due to unsupporting browsers throwing out the :host selector
:host([width='full@small']) {
  @include bolt-mq($until: small) {
    width: 100%;
  }
}

/**
 * Border Radius
 */
@each $radius-name, $radius-value in $bolt-border-radius-values {
  @if $radius-name == 'full' {
    .c-bolt-button--border-radius-#{$radius-name},
    .c-bolt-button--border-radius-#{$radius-name}:before,
    .c-bolt-button--border-radius-#{$radius-name}:after,
    bolt-button[border-radius='#{$radius-name}'] {
      border-radius: $radius-value;
    }
  }
}
/**
 * Icon and text spacing and display
 */
.c-bolt-button__icon {
  display: flex;
}

.c-bolt-button__item {
  display: inline-flex;
  text-align: start;
}

.c-bolt-button__icon-sizer {
  display: flex; // This fixes the spacing issue caused by bolt-icon defined as inline-block.
}

// Spacing only applies when there are BOTH text and icon
.c-bolt-button:not(.c-bolt-button--icon-only) {
  .c-bolt-button__item + .c-bolt-button__icon {
    margin-left: var(--bolt-spacing-x-xxsmall);
  }

  .c-bolt-button__icon + .c-bolt-button__item {
    margin-left: var(--bolt-spacing-x-xxsmall);
  }
}

/**
 * Button Alignments
 */
.c-bolt-button--center {
  justify-content: center;
}

.c-bolt-button--start {
  justify-content: flex-start;
}

.c-bolt-button--end {
  justify-content: flex-end;
}
