import { css } from 'lit';
import componentStyles from '../../styles/component.styles.mjs';

const buttonStyles = css`
  ${componentStyles}

  :host {
    display: inline-block;
    position: relative;
    width: auto;
    cursor: pointer;
  }

  .button {
    display: inline-flex;
    align-items: stretch;
    justify-content: center;
    width: 100%;
    border-style: solid;
    border-width: var(--flow-ui-control-border-width);
    font-family: var(--flow-ui-control-font-family);
    font-weight: var(--flow-ui-font-weight-semibold);
    text-decoration: none;
    text-transform: var(--flow-ui-button-text-transform, uppercase);
    user-select: none;
    white-space: nowrap;
    vertical-align: middle;
    padding: 0;
    transition:
      var(--flow-ui-transition-x-fast) background-color,
      var(--flow-ui-transition-x-fast) color,
      var(--flow-ui-transition-x-fast) border,
      var(--flow-ui-transition-x-fast) box-shadow;
    cursor: inherit;
  }

  .button::-moz-focus-inner {
    border: 0;
  }

  .button:focus {
    outline: none;
  }

  .button:focus-visible {
    outline: var(--flow-ui-focus-ring);
    outline-offset: var(--flow-ui-focus-ring-offset);
  }

  .button--disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }

  /* When disabled, prevent mouse events from bubbling up from children */
  .button--disabled * {
    pointer-events: none;
  }

  .button__prefix,
  .button__suffix {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    pointer-events: none;
  }
  .button__label {
    display: inline-block;
  }

  .button__label::slotted(flow-ui-icon) {
    vertical-align: -2px;
  }

  /*
   * Standard buttons
   */

  /* Default */
  .button--standard.button--default {
    background-color: var(--flow-ui-color-background);
    border-color: var(--flow-ui-color-neutral-600);
    color: var(--flow-ui-color-neutral-800);
  }

  .button--standard.button--default:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-primary-50);
    border-color: var(--flow-ui-color-primary-300);
    color: var(--flow-ui-color-primary-600);
  }

  .button--standard.button--default:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-primary-100);
    border-color: var(--flow-ui-color-primary-300);
    color: var(--flow-ui-color-primary-600);
  }

  /* Primary */
  .button--standard.button--primary {
    background-color: var(--flow-ui-color-primary-500);
    border-color: var(--flow-ui-color-primary-500);
    color: var(--flow-ui-color-primary-contrast);
  }

  .button--standard.button--primary:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-primary-400);
    border-color: var(--flow-ui-color-primary-400);
    color: var(--flow-ui-color-primary-contrast);
  }

  .button--standard.button--primary:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-primary-500);
    border-color: var(--flow-ui-color-primary-500);
    color: var(--flow-ui-color-primary-contrast);
  }

  /* Secondary */
  .button--standard.button--secondary {
    background-color: var(--flow-ui-color-secondary-500);
    border-color: var(--flow-ui-color-secondary-500);
    color: var(--flow-ui-color-secondary-contrast);
  }

  .button--standard.button--secondary:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-secondary-400);
    border-color: var(--flow-ui-color-secondary-400);
    color: var(--flow-ui-color-secondary-contrast);
  }

  .button--standard.button--secondary:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-secondary-500);
    border-color: var(--flow-ui-color-secondary-500);
    color: var(--flow-ui-color-secondary-contrast);
  }

  /* Success */
  .button--standard.button--success {
    background-color: var(--flow-ui-color-success-500);
    border-color: var(--flow-ui-color-success-500);
    color: var(--flow-ui-color-success-contrast);
  }

  .button--standard.button--success:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-success-400);
    border-color: var(--flow-ui-color-success-400);
    color: var(--flow-ui-color-success-contrast);
  }

  .button--standard.button--success:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-success-500);
    border-color: var(--flow-ui-color-success-500);
    color: var(--flow-ui-color-success-contrast);
  }

  /* Neutral */
  .button--standard.button--neutral {
    background-color: var(--flow-ui-color-neutral-500);
    border-color: var(--flow-ui-color-neutral-500);
    color: var(--flow-ui-color-neutral-contrast);
  }

  .button--standard.button--neutral:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-neutral-400);
    border-color: var(--flow-ui-color-neutral-400);
    color: var(--flow-ui-color-neutral-contrast);
  }

  .button--standard.button--neutral:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-neutral-500);
    border-color: var(--flow-ui-color-neutral-500);
    color: var(--flow-ui-color-neutral-contrast);
  }

  /* Warning */
  .button--standard.button--warning {
    background-color: var(--flow-ui-color-warning-500);
    border-color: var(--flow-ui-color-warning-500);
    color: var(--flow-ui-color-warning-contrast);
  }
  .button--standard.button--warning:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-warning-400);
    border-color: var(--flow-ui-color-warning-400);
    color: var(--flow-ui-color-warning-contrast);
  }

  .button--standard.button--warning:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-warning-500);
    border-color: var(--flow-ui-color-warning-500);
    color: var(--flow-ui-color-warning-contrast);
  }

  /* Danger */
  .button--standard.button--danger {
    background-color: var(--flow-ui-color-danger-500);
    border-color: var(--flow-ui-color-danger-500);
    color: var(--flow-ui-color-danger-contrast);
  }

  .button--standard.button--danger:hover:not(.button--disabled) {
    background-color: var(--flow-ui-color-danger-400);
    border-color: var(--flow-ui-color-danger-400);
    color: var(--flow-ui-color-danger-contrast);
  }

  .button--standard.button--danger:active:not(.button--disabled) {
    background-color: var(--flow-ui-color-danger-500);
    border-color: var(--flow-ui-color-danger-500);
    color: var(--flow-ui-color-danger-contrast);
  }

  /*
   * Outline buttons
   */

  .button--outline {
    background: none;
    border: solid 1px;
  }

  /* Default */
  .button--outline.button--default {
    border-color: var(--flow-ui-color-neutral-300);
    color: var(--flow-ui-color-neutral-700);
  }

  .button--outline.button--default:hover:not(.button--disabled),
  .button--outline.button--default.button--checked:not(.button--disabled) {
    border-color: var(--flow-ui-color-primary-500);
    background-color: var(--flow-ui-color-primary-500);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--default:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-primary-600);
    background-color: var(--flow-ui-color-primary-600);
    color: var(--flow-ui-color-background);
  }

  /* Primary */
  .button--outline.button--primary {
    border-color: var(--flow-ui-color-primary-500);
    color: var(--flow-ui-color-primary-500);
  }

  .button--outline.button--primary:hover:not(.button--disabled),
  .button--outline.button--primary.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-primary-500);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--primary:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-primary-600);
    background-color: var(--flow-ui-color-primary-600);
    color: var(--flow-ui-color-background);
  }

  /* Secondary */
  .button--outline.button--secondary {
    border-color: var(--flow-ui-color-secondary-500);
    color: var(--flow-ui-color-secondary-500);
  }

  .button--outline.button--secondary:hover:not(.button--disabled),
  .button--outline.button--secondary.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-secondary-500);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--secondary:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-secondary-600);
    background-color: var(--flow-ui-color-secondary-600);
    color: var(--flow-ui-color-background);
  }

  /* Success */
  .button--outline.button--success {
    border-color: var(--flow-ui-color-success-600);
    color: var(--flow-ui-color-success-600);
  }

  .button--outline.button--success:hover:not(.button--disabled),
  .button--outline.button--success.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-success-600);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--success:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-success-700);
    background-color: var(--flow-ui-color-success-700);
    color: var(--flow-ui-color-background);
  }

  /* Neutral */
  .button--outline.button--neutral {
    border-color: var(--flow-ui-color-neutral-600);
    color: var(--flow-ui-color-neutral-600);
  }

  .button--outline.button--neutral:hover:not(.button--disabled),
  .button--outline.button--neutral.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-neutral-600);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--neutral:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-neutral-700);
    background-color: var(--flow-ui-color-neutral-700);
    color: var(--flow-ui-color-background);
  }

  /* Warning */
  .button--outline.button--warning {
    border-color: var(--flow-ui-color-warning-600);
    color: var(--flow-ui-color-warning-600);
  }

  .button--outline.button--warning:hover:not(.button--disabled),
  .button--outline.button--warning.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-warning-600);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--warning:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-warning-700);
    background-color: var(--flow-ui-color-warning-700);
    color: var(--flow-ui-color-background);
  }

  /* Danger */
  .button--outline.button--danger {
    border-color: var(--flow-ui-color-danger-600);
    color: var(--flow-ui-color-danger-600);
  }

  .button--outline.button--danger:hover:not(.button--disabled),
  .button--outline.button--danger.button--checked:not(.button--disabled) {
    background-color: var(--flow-ui-color-danger-600);
    color: var(--flow-ui-color-background);
  }

  .button--outline.button--danger:active:not(.button--disabled) {
    border-color: var(--flow-ui-color-danger-700);
    background-color: var(--flow-ui-color-danger-700);
    color: var(--flow-ui-color-background);
  }

  @media (forced-colors: active) {
    .button.button--outline.button--checked:not(.button--disabled) {
      outline: solid 2px transparent;
    }
  }

  /*
   * Text buttons
   */

  .button--text {
    background-color: transparent;
    border-color: transparent;
    color: var(--flow-ui-color-primary-500);
  }

  .button--text:hover:not(.button--disabled) {
    background-color: transparent;
    border-color: transparent;
    color: var(--flow-ui-color-primary-400);
  }

  .button--text:focus-visible:not(.button--disabled) {
    background-color: transparent;
    border-color: transparent;
    color: var(--flow-ui-color-primary-400);
  }

  .button--text:active:not(.button--disabled) {
    background-color: transparent;
    border-color: transparent;
    color: var(--flow-ui-color-primary-600);
  }

  /*
   * Size modifiers
   */

  .button--x-small {
    height: auto;
    min-height: var(--flow-ui-control-height-small);
    font-size: var(--flow-ui-button-font-size-x-small);
    line-height: calc(var(--flow-ui-control-height-small) - var(--flow-ui-control-border-width) * 2);
    border-radius: var(--flow-ui-control-border-radius-small);
  }

  .button--small {
    height: auto;
    min-height: var(--flow-ui-control-height-small);
    font-size: var(--flow-ui-button-font-size-small);
    line-height: calc(var(--flow-ui-control-height-small) - var(--flow-ui-control-border-width) * 2);
    border-radius: var(--flow-ui-control-border-radius-small);
  }

  .button--medium {
    height: auto;
    min-height: var(--flow-ui-control-height-medium);
    font-size: var(--flow-ui-button-font-size-medium);
    line-height: calc(var(--flow-ui-control-height-medium) - var(--flow-ui-control-border-width) * 2);
    border-radius: var(--flow-ui-control-border-radius-medium);
  }

  .button--large {
    height: auto;
    min-height: var(--flow-ui-control-height-large);
    font-size: var(--flow-ui-button-font-size-large);
    line-height: calc(var(--flow-ui-control-height-large) - var(--flow-ui-control-border-width) * 2);
    border-radius: var(--flow-ui-control-border-radius-large);
  }

  /*
   * Pill modifier
   */

  .button--pill.button--small {
    border-radius: var(--flow-ui-control-height-small);
  }

  .button--pill.button--medium {
    border-radius: var(--flow-ui-control-height-medium);
  }

  .button--pill.button--large {
    border-radius: var(--flow-ui-control-height-large);
  }

  /*
   * FAB modifier
   */

  .button--fab {
    padding-left: 0;
    padding-right: 0;
  }

  .button--fab .button__label {
    display: flex;
    align-items: center;
  }

  .button--fab.button--small {
    width: var(--flow-ui-control-height-small);
    box-shadow: var(--flow-ui-shadow-small);
    font-size: var(--flow-ui-font-size-medium);
  }

  .button--fab.button--medium {
    width: var(--flow-ui-control-height-medium);
    box-shadow: var(--flow-ui-shadow-medium);
    font-size: var(--flow-ui-font-size-large);
  }

  .button--fab.button--large {
    width: var(--flow-ui-control-height-large);
    box-shadow: var(--flow-ui-shadow-large);
    font-size: var(--flow-ui-font-size-large);
  }

  .button--fab .button__prefix,
  .button--fab .button__suffix,
  .button--fab .button__caret {
    display: none;
  }

  .button--circle {
    border-radius: 50%;
  }

  .button--square.button--small {
    border-radius: var(--flow-ui-border-radius-small);
  }

  .button--square.button--medium {
    border-radius: var(--flow-ui-border-radius-medium);
  }

  .button--square.button--large {
    border-radius: var(--flow-ui-border-radius-large);
  }

  /*
   * Caret modifier
   */

  .button--caret .button__suffix {
    display: none;
  }

  .button--caret .button__caret {
    height: auto;
  }

  /*
   * Loading modifier
   */

  .button--loading {
    position: relative;
    cursor: wait;
  }

  .button--loading .button__prefix,
  .button--loading .button__label,
  .button--loading .button__suffix,
  .button--loading .button__caret {
    visibility: hidden;
  }

  .button--loading flow-ui-progress-ring {
    position: absolute;
  }

  .button--medium flow-ui-progress-ring,
  .button--large flow-ui-progress-ring {
    top: calc(50% - 0.75em);
    left: calc(50% - 0.75em);
  }

  .button--small flow-ui-progress-ring {
    top: calc(50% - 0.5em);
    left: calc(50% - 0.5em);
  }

  .button--loading flow-ui-progress-ring::part(base) {
    --indicator-color: currentColor;
  }

  /*
   * Badges
   */

  .button ::slotted(flow-ui-badge) {
    position: absolute;
    top: 0;
    right: 0;
    translate: 50% -50%;
    pointer-events: none;
  }

  .button--rtl ::slotted(flow-ui-badge) {
    right: auto;
    left: 0;
    translate: -50% -50%;
  }

  /*
   * Button spacing
   */

  .button--has-label.button--small .button__label {
    padding: 0 var(--flow-ui-spacing-small);
  }

  .button--has-label.button--medium .button__label {
    padding: 0 var(--flow-ui-spacing-medium);
  }

  .button--has-label.button--large .button__label {
    padding: 0 var(--flow-ui-spacing-large);
  }

  .button--has-prefix.button--small {
    padding-inline-start: var(--flow-ui-spacing-x-small);
  }

  .button--has-prefix.button--small .button__label {
    padding-inline-start: var(--flow-ui-spacing-x-small);
  }

  .button--has-prefix.button--medium {
    padding-inline-start: var(--flow-ui-spacing-small);
  }

  .button--has-prefix.button--medium .button__label {
    padding-inline-start: var(--flow-ui-spacing-small);
  }

  .button--has-prefix.button--large {
    padding-inline-start: var(--flow-ui-spacing-small);
  }

  .button--has-prefix.button--large .button__label {
    padding-inline-start: var(--flow-ui-spacing-small);
  }

  .button--has-suffix.button--small,
  .button--caret.button--small {
    padding-inline-end: var(--flow-ui-spacing-x-small);
  }

  .button--has-suffix.button--small .button__label,
  .button--caret.button--small .button__label {
    padding-inline-end: var(--flow-ui-spacing-x-small);
  }

  .button--has-suffix.button--medium,
  .button--caret.button--medium {
    padding-inline-end: var(--flow-ui-spacing-small);
  }

  .button--has-suffix.button--medium .button__label,
  .button--caret.button--medium .button__label {
    padding-inline-end: var(--flow-ui-spacing-small);
  }

  .button--has-suffix.button--large,
  .button--caret.button--large {
    padding-inline-end: var(--flow-ui-spacing-small);
  }

  .button--has-suffix.button--large .button__label,
  .button--caret.button--large .button__label {
    padding-inline-end: var(--flow-ui-spacing-small);
  }

  /*
   * Button groups support a variety of button types (e.g. buttons with tooltips, buttons as dropdown triggers, etc.).
   * This means buttons aren't always direct descendants of the button group, thus we can't target them with the
   * ::slotted selector. To work around this, the button group component does some magic to add these special classes to
   * buttons and we style them here instead.
   */

  :host(
      .flow-ui-button-group__button--first:not(.flow-ui-button-group__button--last):not(
          .flow-ui-button-group__button--vertical
        )
    )
    .button {
    border-start-end-radius: 0;
    border-end-end-radius: 0;
  }

  :host(.flow-ui-button-group__button--inner) .button {
    border-radius: 0;
  }

  :host(
      .flow-ui-button-group__button--last:not(
          .flow-ui-button-group__button--vertical,
          .flow-ui-button-group__button--first
        )
    )
    .button {
    border-start-start-radius: 0;
    border-end-start-radius: 0;
  }

  /* All except the first */
  :host(
      .flow-ui-button-group__button:not(.flow-ui-button-group__button--vertical, .flow-ui-button-group__button--first)
    ) {
    margin-inline-start: calc(-1 * var(--flow-ui-control-border-width));
  }

  /* Add a visual separator between solid buttons */
  :host(
      .flow-ui-button-group__button:not(
          .flow-ui-button-group__button--first,
          .flow-ui-button-group__button--radio,
          .flow-ui-button-group__button--tool,
          .flow-ui-button-group__button--vertical [variant='default']
        ):not(:hover)
    )
    .button:after {
    content: '';
    position: absolute;
    top: 0;
    inset-inline-start: 0;
    bottom: 0;
    border-left: solid 1px rgb(128 128 128 / 33%);
    mix-blend-mode: multiply;
  }

  /* Variants for vertical layout of button group */
  :host(
      .flow-ui-button-group__button--vertical.flow-ui-button-group__button--first:not(
          .flow-ui-button-group__button--last
        )
    )
    .button {
    border-end-start-radius: 0;
    border-end-end-radius: 0;
  }

  :host(.flow-ui-button-group__button--vertical:not(.flow-ui-button-group__button--first)) .button {
    border-start-start-radius: 0;
    border-start-end-radius: 0;
  }

  :host(.flow-ui-button-group__button--vertical:not(.flow-ui-button-group__button--first)) {
    margin-block-start: calc(-1 * var(--flow-ui-control-border-width));
  }

  /* Bump hovered, focused, and checked buttons up so their focus ring isn't clipped */
  :host(.flow-ui-button-group__button--hover) {
    z-index: 1;
  }

  /* Focus and checked are always on top */
  :host(.flow-ui-button-group__button[checked]) {
    z-index: 2;
  }

  :host(.flow-ui-button-group__button--focus),
  :host(.flow-ui-button-group__button--focus[checked]) {
    z-index: 3;
  }
`;

export { buttonStyles as default };
