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;
    --control-height: var(--flow-ui-control-height-medium);
  }

  /** Note that input height needs to be available on host element for height calculations in render method */

  :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 */
  .select {
    flex: 1 1 auto;
    display: inline-flex;
    width: 100%;
    position: relative;
    vertical-align: middle;
  }

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

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

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

  /* Combobox */
  .select__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;
    cursor: pointer;
    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);
  }

  .select__display-input {
    position: relative;
    width: 100%;
    font: inherit;
    border: none;
    background: none;
    color: var(--flow-ui-control-color);
    cursor: inherit;
    overflow: hidden;
    padding: 0;
    margin: 0;
    -webkit-appearance: none;
    text-overflow: ellipsis;
  }

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

  .select--inline .select__display-input-wrapper {
    grid-template-columns: min-content;
  }

  /* For measuring width of displayed option text */
  .select__display-input-sizer {
    white-space: pre;
    height: 0;
    overflow: hidden;
  }

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

  .form-control.form-control--disabled .select__display-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);
  }

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

  .select__display-input:focus {
    outline: none;
  }

  /* Visually hide the display input when multiple is enabled */
  .select--multiple:not(.select--placeholder-visible) .select__display-input-wrapper {
    position: absolute;
    z-index: -1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
  }

  .select--multiple:not(.select--placeholder-visible) .select__combobox {
    padding-block: 2px;
    padding-inline-start: 0;
  }

  .select__value-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    padding: 0;
    margin: 0;
    opacity: 0;
    z-index: -1;
  }

  .select__tags {
    display: flex;
    flex: 1;
    align-items: center;
    flex-wrap: wrap;
    margin-inline-start: var(--control-spacing);
    gap: 2px;
  }

  .select__tags flow-ui-tag {
    --background-color: transparent;
    --border-color: transparent;
    cursor: pointer;
  }

  .select__tags flow-ui-tag::part(base) {
    padding: 0;
  }

  .select__tags flow-ui-tag::part(remove-button) {
    margin: 0;
  }

  .select--disabled .select__tags,
  .select--disabled .select__tags flow-ui-tag {
    cursor: not-allowed !important;
  }

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

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

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

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

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

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

  /* Clear button */
  .select__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-start: var(--control-spacing);
    transition: var(--flow-ui-transition-fast) color;
    cursor: pointer;
  }

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

  .select__clear:focus {
    outline: none;
  }

  /* Expand icon */
  .select__expand-icon {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    transition: var(--flow-ui-transition-medium) rotate ease;
    rotate: 0;
    margin-inline-start: var(--flow-ui-spacing-small);
  }

  .select--open .select__expand-icon {
    rotate: -180deg;
  }

  .select--inline.select--open .select__expand-icon {
    rotate: 0deg;
  }

  /* Listbox */
  .select__listbox {
    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);
  }

  .select__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);
  }

  .select__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 };
