import { css } from 'lit';
import componentStyles from '../../styles/component.styles.mjs';
import formControlStyles from '../../styles/form-control.styles.mjs';

const styles = css`
  ${componentStyles}
  ${formControlStyles}

  :host {
    display: inline-block;
    width: 100%;

    --control-height: var(--flow-ui-control-height-medium);
  }

  :host([size='small']) {
    --control-height: var(--flow-ui-control-height-small);
  }

  :host([size='large']) {
    --control-height: var(--flow-ui-control-height-large);
  }

  .form-control .form-control__input {
    height: unset;
    min-height: var(--control-height);
  }

  /** The popup */
  .autocomplete {
    flex: 1 1 auto;
    display: inline-flex;
    width: 100%;
    position: relative;
    vertical-align: middle;
  }

  .autocomplete::part(popup) {
    z-index: var(--flow-ui-z-index-dropdown);
  }

  .autocomplete[data-current-placement^='top']::part(popup) {
    transform-origin: bottom;
  }

  .autocomplete[data-current-placement^='bottom']::part(popup) {
    transform-origin: top;
  }

  /* Combobox */
  .autocomplete__combobox {
    flex: 1;
    display: flex;
    width: 100%;
    min-width: 0;
    position: relative;
    align-items: center;
    justify-content: start;
    font-family: var(--flow-ui-control-font-family);
    font-weight: var(--flow-ui-control-font-weight);
    letter-spacing: var(--flow-ui-control-letter-spacing);
    vertical-align: middle;
    overflow: hidden;
    transition:
      var(--flow-ui-transition-fast) color,
      var(--flow-ui-transition-fast) border,
      var(--flow-ui-transition-fast) box-shadow,
      var(--flow-ui-transition-fast) background-color;

    border-radius: var(--control-border-radius);
    font-size: var(--control-font-size);
    min-height: var(--control-height);
    padding-block: 0;
    padding-inline: var(--control-spacing);
  }

  .form-control--disabled .autocomplete__combobox {
    cursor: not-allowed;
  }

  .autocomplete__input {
    flex: 1;
    font-family: inherit;
    font-size: inherit;
    font-weight: inherit;
    width: 100%;
    min-width: 0;
    height: var(--control-height);
    color: var(--flow-ui-control-color);
    border: none;
    background: none;
    box-shadow: none;
    padding: 0;
    margin: 0;
    cursor: inherit;
    -webkit-appearance: none;
    outline: none;
  }

  /* Wrapper to allow display input to be sized based on its content */
  .autocomplete__input-wrapper {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr;
  }

  .autocomplete--inline .autocomplete__input-wrapper {
    grid-template-columns: min-content;
  }

  .autocomplete__input-sizer {
    white-space: pre;
    height: 0;
    overflow: hidden;
  }

  .autocomplete__input::placeholder {
    color: var(--flow-ui-control-placeholder-color);
  }

  .form-control.form-control--disabled .autocomplete__input::placeholder {
    color: var(--flow-ui-control-placeholder-color-disabled);
  }

  .form-control.form-control--disabled .select__display-input {
    color: var(--flow-ui-control-color-disabled);
  }

  .autocomplete__input::not(.autocomplete--disabled):hover .autocomplete__input {
    color: var(--flow-ui-control-color-hover);
  }

  .autocomplete__input:focus {
    outline: none;
  }

  /* Pills */
  .autocomplete--pill.autocomplete--small .autocomplete__combobox {
    border-radius: var(--flow-ui-control-height-small);
  }

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

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

  /* Prefix */
  .autocomplete__prefix {
    flex: 0;
    display: inline-flex;
    align-items: center;
    color: var(--flow-ui-control-placeholder-color);
  }

  .autocomplete__prefix::slotted(*) {
    margin-inline-end: var(--control-spacing);
  }

  /* Suffix */
  .autocomplete__suffix {
    flex: 0;
    display: inline-flex;
    align-items: center;
    color: var(--flow-ui-control-placeholder-color);
  }

  /* Clear btn */
  .autocomplete__clear {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: inherit;
    color: var(--flow-ui-control-icon-color);
    border: none;
    background: none;
    padding: 0;
    margin-inline-end: var(--control-spacing);
    transition: var(--flow-ui-transition-fast) color;
    cursor: pointer;
  }

  .autocomplete__clear:hover {
    color: var(--flow-ui-control-icon-color-hover);
  }

  .autocomplete__clear:focus {
    outline: none;
  }

  /* Listbox */
  .autocomplete__listbox {
    transition-timing-function: ease-in;

    display: block;
    position: relative;
    font-family: var(--flow-ui-font-sans);
    font-size: var(--flow-ui-font-size-medium);
    font-weight: var(--flow-ui-font-weight-normal);
    box-shadow: var(--flow-ui-shadow-large);
    background: var(--flow-ui-menu-background-color);
    color: var(--flow-ui-menu-color);
    border: solid var(--flow-ui-menu-border-width) var(--flow-ui-menu-border-color);
    border-radius: var(--flow-ui-menu-border-radius);
    padding-block: var(--flow-ui-spacing-x-small);
    padding-inline: 0;
    overflow: auto;
    overscroll-behavior: none;

    /* Make sure it adheres to the popup's auto size */
    max-width: var(--auto-size-available-width);
    max-height: var(--auto-size-available-height);
  }

  .autocomplete__listbox ::slotted(flow-ui-divider) {
    --spacing: var(--flow-ui-spacing-x-small);
    --inset: var(--flow-ui-spacing-medium);
    --color: var(--flow-ui-menu-divider-color);
  }

  .autocomplete__listbox ::slotted(small) {
    display: block;
    font-size: var(--flow-ui-menu-divider-font-size);
    font-weight: var(--flow-ui-font-weight-semibold);
    color: var(--flow-ui-menu-divider-text-color);
    padding-block: var(--flow-ui-spacing-2x-small);
    padding-inline: var(--flow-ui-spacing-medium);
  }
`;

export { styles as default };
