@use '../utils';

.str-chat {
  /* The width/height of the message options button(s), for Angular SDK it's only used on desktop devices starting from version 5 */
  --str-chat__message-options-button-size: calc(var(--str-chat__spacing-px) * 26);

  /* The maximum allowed width of the message component */
  --str-chat__message-max-width: calc(var(--str-chat__spacing-px) * 480);

  /* The maximum allowed width of the message component, if it has attachments */
  --str-chat__message-with-attachment-max-width: calc(var(--str-chat__spacing-px) * 300);

  /* The maximum allowed width of quoted messages inside the message component */
  --str-chat__quoted-message-max-width: calc(var(--str-chat__spacing-px) * 250);

  /* The maximum allowed width of quoted messages inside the message input component */
  --str-chat__quoted-message-inside-message-input-max-width: calc(
    var(--str-chat__spacing-px) * 200
  );

  /* The maximum allowed height of quoted messages inside the message input component */
  --str-chat__quoted-message-inside-message-input-max-height: calc(
    var(--str-chat__quoted-message-inside-message-input-max-width) +
      calc(var(--str-chat__spacing-px) * 50)
  );

  /* The maximum number of lines displayed for quoted messages, set to "none" to turn off clamping */
  --str-chat__quoted-message-line-clamp: 5;
}

.str-chat__message {
  --str-chat-message-options-size: calc(3 * var(--str-chat__message-options-button-size));

  &.str-chat__message-without-touch-support {
    --str-chat-message-options-size: calc(1 * var(--str-chat__message-options-button-size));
  }

  .str-chat__message-bubble {
    max-width: var(--str-chat__message-max-width);
  }

  .str-chat__message-options {
    --str-chat-icon-height: calc(var(--str-chat__message-options-button-size) * 0.7);
  }
}

.str-chat__message.str-chat__message--has-attachment {
  --str-chat__message-max-width: var(--str-chat__message-with-attachment-max-width);

  .str-chat__message-bubble {
    max-width: var(--str-chat__message-max-width);
  }
}

.str-chat__quoted-message-preview {
  --str-chat__message-max-width: var(--str-chat__quoted-message-max-width);

  .str-chat__quoted-message-bubble {
    // Turn off pointer events so they don't interfere with jump to message
    pointer-events: none;
    max-width: var(--str-chat__message-max-width);
  }
}

.str-chat__quoted-message-text-value {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: var(--str-chat__quoted-message-line-clamp);
  line-clamp: var(--str-chat__quoted-message-line-clamp);
  -webkit-box-orient: vertical;
}

.str-chat__message,
.str-chat__quoted-message-preview {
  @mixin chat-bubble-spacing {
    padding: var(--str-chat__spacing-2) var(--str-chat__spacing-4);

    p {
      white-space: pre-line;
      margin: 0;
    }
  }

  // Using grid layout so we can use template from theme-v1
  display: grid;
  word-wrap: break-word;
  word-break: break-word;
  hyphens: auto;
  overflow-wrap: break-word;

  .str-chat__message-reminder {
    grid-area: message-reminder;
    padding-block: 0.5rem 0.125rem;
    margin: 0;
  }

  &.str-chat__message--other,
  &.str-chat__quoted-message-preview {
    grid-template-areas:
      '. message-reminder'
      'avatar message'
      '. replies'
      '. translation-notice'
      '. custom-metadata'
      '. metadata';
    column-gap: var(--str-chat__spacing-2);
    grid-template-columns: auto 1fr;
    justify-items: flex-start;
  }

  &.str-chat__message--me {
    grid-template-areas:
      'message-reminder'
      'message'
      'replies'
      'translation-notice'
      'custom-metadata'
      'metadata';
    justify-items: end;

    // > selector added to not hide sender of inline replies
    > .str-chat__message-sender-avatar {
      display: none;
    }
  }

  &.str-chat__message--deleted {
    grid-template-areas: 'message';
  }

  &.str-chat__message--blocked {
    grid-template-areas: 'message';
  }

  &.str-chat__message--system {
    grid-template-areas: 'message';
    grid-template-columns: auto;
  }

  .str-chat__message-sender-avatar {
    grid-area: avatar;
    align-self: end;
  }

  .str-chat__message-inner {
    grid-area: message;
    display: grid;
    grid-template-areas:
      'reactions reactions'
      'message-bubble options';
    grid-template-columns: auto 1fr;
    column-gap: var(--str-chat__spacing-2);
    position: relative;

    .str-chat__message-options {
      grid-area: options;
      align-items: flex-start;
      justify-content: flex-end;
      flex-direction: row-reverse;
      width: var(--str-chat-message-options-size);

      .str-chat__message-actions-box-button,
      .str-chat__message-reply-in-thread-button,
      .str-chat__message-reactions-button {
        @include utils.flex-row-center;
        cursor: pointer;
        width: var(--str-chat__message-options-button-size);
        height: var(--str-chat__message-options-button-size);
      }

      .str-chat__message-actions-box-button,
      .str-chat__message-actions-container {
        position: relative;
      }
    }

    .str-chat__message-reactions-host {
      grid-area: reactions;
    }

    .str-chat__message-bubble {
      grid-area: message-bubble;
      position: relative;
      min-width: 0;

      .str-chat__message-text {
        @include chat-bubble-spacing;

        .str-chat__message-text-inner {
          @include utils.prevent-glitch-text-overflow;
        }
      }
    }
  }

  &.str-chat__message--me .str-chat__message-inner {
    grid-template-areas:
      'reminder reminder'
      'reactions reactions'
      'options message-bubble';
    grid-template-columns: 1fr auto;

    .str-chat__message-options {
      flex-direction: row;
    }
  }

  .str-chat__translation-notice {
    grid-area: translation-notice;

    button {
      cursor: pointer;
      padding: var(--str-chat__spacing-1) 0;
      margin: 0;
    }
  }

  .str-chat__custom-message-metadata {
    grid-area: custom-metadata;
    margin-block-start: var(--str-chat__spacing-0_5);
  }

  .str-chat__message-metadata {
    grid-area: metadata;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    column-gap: var(--str-chat__spacing-1);
    margin-block-start: var(--str-chat__spacing-0_5);

    .str-chat__message-simple-name {
      @include utils.prevent-glitch-text-overflow;
    }

    .str-chat__message-simple-timestamp + .str-chat__mesage-simple-edited::before {
      content: '•';
      margin-right: var(--str-chat__spacing-1);
    }

    .str-chat__message-edited-timestamp {
      --str-chat__message-edited-timestamp-height: 1rem;
      flex-basis: 100%;
    }
  }

  &.str-chat__message--me .str-chat__message-metadata {
    justify-content: flex-end;
    text-align: right;
  }

  .str-chat__message-status {
    --str-chat-icon-height: calc(var(--str-chat__spacing-px) * 16);
    @include utils.flex-row-center;
    column-gap: var(--str-chat__spacing-0_5);
    position: relative;

    svg {
      width: var(--str-chat-icon-height);
      height: var(--str-chat-icon-height);
    }
  }

  .str-chat__message-status.str-chat__message-status-sent {
    --str-chat-icon-height: calc(var(--str-chat__spacing-px) * 12);

    svg {
      width: var(--str-chat-icon-height);
      height: var(--str-chat-icon-height);
    }
  }

  .str-chat__message-status-sent {
    --str-chat-icon-height: calc(var(--str-chat__spacing-px) * 12);

    svg {
      width: var(--str-chat-icon-height);
      height: var(--str-chat-icon-height);
    }
  }

  .str-chat__message-status-delivered {
    --str-chat-icon-height: calc(var(--str-chat__spacing-px) * 15);

    svg {
      width: var(--str-chat-icon-height);
      height: var(--str-chat-icon-height);
    }
  }

  .str-chat__message-replies-count-button-wrapper,
  .str-chat__message-is-thread-reply-button-wrapper {
    grid-area: replies;

    button {
      cursor: pointer;
      padding: 0;
    }

    button:disabled {
      cursor: auto;
    }
  }

  .str-chat__message--deleted-inner {
    @include chat-bubble-spacing;
  }

  .str-chat__message--blocked-inner {
    @include chat-bubble-spacing;
  }

  .str-chat__quoted-message-bubble {
    @include chat-bubble-spacing;
    display: flex;
    column-gap: var(--str-chat__spacing-1);
    min-width: 0;
    width: 100%;
  }

  // Fixes emoji display in Chrome on non-retina display (only detected in Angular SDK) https://bugs.chromium.org/p/chromium/issues/detail?id=596223
  .str-chat__emoji-display-fix {
    display: inline-block;
    width: 1.25em;
  }

  .str-chat__message-error-icon {
    display: none;
  }

  .str-chat__simple-message--error-failed {
    cursor: pointer;
  }

  &.str-chat__message--error,
  &.str-chat__message--failed {
    $icon-size: calc(var(--str-chat__spacing-px) * 18);

    .str-chat__message-error-icon {
      display: block;
      position: absolute;
      bottom: 0;
      inset-inline-end: calc(-1 * calc(#{$icon-size} / 2));

      svg {
        width: $icon-size;
        height: $icon-size;
      }
    }
  }
}

// Message options display - default mode: they appear when .str-chat__li is hovered
.str-chat__ul:not(.str-chat__message-options-in-bubble, .str-chat__message-with-touch-support),
.str-chat__virtual-list:not(.str-chat__message-options-in-bubble, .str-chat__message-with-touch-support) {
  /* This rule won't be applied in browsers that don't support :has() */
  .str-chat__li:hover:not(:has(.str-chat__reaction-list:hover, .str-chat__modal--open)),
  .str-chat__li:focus-within:not(:has(.str-chat__reaction-list:focus-within, .str-chat__modal--open)),
  .str-chat__li:has(.str-chat__message-options--active) {
    .str-chat__message-options {
      display: flex;
    }

    .str-chat__message--other .str-chat__message-inner {
      margin-inline-end: 0;
    }

    .str-chat__message--me .str-chat__message-inner {
      margin-inline-start: 0;
    }
  }

  /* Fallback for when :has() is unsupported */
  @supports not selector(:has(a, b)) {
    .str-chat__li:hover,
    .str-chat__li:focus-within {
      .str-chat__message-options {
        display: flex;
      }

      .str-chat__message--other .str-chat__message-inner {
        margin-inline-end: 0;
      }

      .str-chat__message--me .str-chat__message-inner {
        margin-inline-start: 0;
      }
    }
  }
}

// Message options display - second mode: they appear when .str-chat__message-inner is hovered
.str-chat__ul.str-chat__message-options-in-bubble,
.str-chat__virtual-list.str-chat__message-options-in-bubble {
  .str-chat__message-inner {
    &:hover {
      .str-chat__message-options {
        display: flex;
      }
    }
  }

  .str-chat__message--other .str-chat__message-inner:hover {
    margin-inline-end: 0;
  }

  .str-chat__message--me .str-chat__message-inner:hover {
    margin-inline-start: 0;
  }
}

// Message options display - third mode: they appear, when explicitly marked as active
.str-chat__message-inner {
  .str-chat__message-options.str-chat__message-options--active {
    display: flex;
  }
}

.str-chat__message-inner {
  .str-chat__message-options {
    display: none;
  }

  .str-chat__message-actions-open.str-chat__message-options {
    display: flex;
  }
}

.str-chat__message-inner:focus-within {
  .str-chat__message-options {
    display: flex;
  }
}

.str-chat__message--other .str-chat__message-inner:not(:has(.str-chat__message-options--active)) {
  margin-inline-end: var(--str-chat-message-options-size);
}

.str-chat__message--me .str-chat__message-inner {
  margin-inline-start: var(--str-chat-message-options-size);
}

.str-chat__li--middle,
.str-chat__li--top {
  .str-chat__message {
    margin-block-end: var(--str-chat__spacing-0_5);

    .str-chat__message-metadata {
      display: none;
    }

    // > selector added to not hide sender of inline replies
    > .str-chat__message-sender-avatar {
      visibility: hidden;
    }
  }
}

.str-chat__li--bottom,
.str-chat__li--single {
  .str-chat__message {
    margin-block-end: var(--str-chat__spacing-2);
  }
}

.str-chat__date-separator {
  display: flex;
  padding: var(--str-chat__spacing-8);
  align-items: center;

  &-line {
    flex: 1;
    height: var(--str-chat__spacing-px);
  }

  > * {
    &:not(:last-child) {
      margin-right: var(--str-chat__spacing-4);
    }
  }
}

.str-chat__message {
  .str-chat__quoted-message-preview {
    margin-block-end: var(--str-chat__spacing-2);
  }
}

.str-chat__message--system {
  width: 100%;
  text-align: center;

  p {
    margin: 0;
  }
}

.str-chat__quoted-message-preview {
  .str-chat__quoted-message-bubble {
    flex-direction: column;
    align-items: stretch;
    row-gap: var(--str-chat__spacing-1);
    flex-basis: min-content;
  }
}

.str-chat__message-input {
  .str-chat__quoted-message-preview {
    --str-chat__message-max-width: var(--str-chat__quoted-message-inside-message-input-max-width);

    .str-chat__quoted-message-bubble {
      max-height: var(--str-chat__quoted-message-inside-message-input-max-height);

      .str-chat__quoted-message-text {
        max-height: 100%;
        min-height: 0;
        overflow: hidden;

        p {
          max-height: 100%;
          overflow-y: auto;
          overflow-x: hidden;
        }
      }
    }
  }
}

.str-chat__unread-messages-separator-wrapper {
  padding-block: 0.5rem;

  .str-chat__unread-messages-separator {
    @include utils.flex-row-center;
    width: 100%;
    padding: 0.5rem;
  }
}

.str-chat__unread-messages-notification {
  --str-chat-icon-height: calc(var(--str-chat__spacing-px) * 16);
  position: absolute;
  top: 0.75rem;
  z-index: 2;
  display: flex;
  align-items: center;
  overflow: clip;

  button {
    padding-block: 0.5rem;
    height: 100%;
    width: 100%;
    white-space: nowrap;
  }

  button:first-of-type {
    padding-inline: 0.75rem 0.375rem;
  }

  button:last-of-type {
    padding-inline: 0.375rem 0.75rem;

    svg {
      width: 0.875rem;
    }
  }
}

.str-chat-angular__message-bubble {
  /* transform: translate3d(0, 0, 0) fixes scrolling issues on iOS, see: https://stackoverflow.com/questions/50105780/elements-disappear-when-scrolling-in-safari-webkit-transform-fix-only-works-t/50144295#50144295 */
  transform: translate3d(0, 0, 0);

  &.str-chat-angular__message-bubble--attachment-modal-open {
    /* transform: none is required when image carousel is open in order for the modal to be correctly positioned, see how the transform property changes the behavior of fixed positioned elements https://developer.mozilla.org/en-US/docs/Web/CSS/position  */
    transform: none;
  }
}

.str-chat__message-edited-timestamp {
  overflow: hidden;
  transition: height 0.1s;

  &--open {
    height: var(--str-chat__message-edited-timestamp-height, 1rem);
  }

  &--collapsed {
    height: 0;
  }
}

.str-chat__message-text--pointer-cursor {
  cursor: pointer;
}

.str-chat__message-with-touch-support {
  .str-chat__message-bubble {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
  }

  &.str-chat__message-menu-opened {
    .str-chat__attachments-container,
    .str-chat__message-text-inner {
      pointer-events: none;
    }
  }

  .str-chat__message-inner {
    margin-inline: 0;
  }

  .str-chat__message-options {
    display: none;
  }

  .stream-chat-angular__image-modal-host {
    -webkit-touch-callout: default;
  }
}
