@mixin clamped-height-from-original-image-dimensions($max-height-css-var, $max-width-css-var) {
  height: min(
    var(#{$max-height-css-var}),
    min(var(#{$max-width-css-var}, 1000000) / var(--original-width, 1000000), 1px) *
      var(--original-height, 1000000)
  );
}

.str-chat__attachment,
.str-chat__message-attachment {
  --attachment-max-width: 450px;
  --video-width: 300px;

  max-width: var(--attachment-max-width);
}

.str-chat__message-attachment.str-chat__message-attachment--video:not(.str-chat__message-attachment--card) {
  --attachment-max-width: var(--video-width);
  width: var(--video-width);
}

.str-chat__player-wrapper {
  position: relative;
  padding-top: 56.25%; /* Player ratio: 100 / (1280 / 720) */
  display: flex;
  flex-direction: column;

  .react-player,
  .str-chat__video-angular {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 0;
    max-height: 100%;
  }
}

.str-chat__message-attachment-dynamic-size {
  --video-max-height: 300px;

  max-height: var(--video-max-height);

  .str-chat__player-wrapper {
    @include clamped-height-from-original-image-dimensions(
      '--video-max-height',
      '--attachment-max-width'
    );
    padding-top: 0;
    position: static;
    // CDN resize requires to have max-height/height and max-width set on this element - React
    max-height: var(--video-max-height);
    max-width: var(--attachment-max-width);

    .str-chat__video-angular,
    .react-player {
      position: static;
      // CDN resize requires to have max-height/height and max-width set on this element - Angular
      max-height: var(--video-max-height);
      max-width: var(--attachment-max-width);
    }
  }
}

/* border radius for attachments */
.str-chat__message .str-chat__player-wrapper .react-player {
  border-radius: var(--border-radius) var(--border-radius) var(--border-radius) 0;
  overflow: hidden;
}

.str-chat__message--me .str-chat__player-wrapper .react-player {
  border-radius: var(--border-radius) var(--border-radius) 0 var(--border-radius);
  overflow: hidden;
}

.str-chat__message {
  &-attachment {
    --attachment-max-width: 375px;
    width: 100%;

    border-radius: var(--border-radius);
    padding: var(--xs-m) auto var(--xs-m) 0;
  }

  /** Let giphies stretch their containers */
  &-attachment--giphy {
    max-width: unset;
  }

  .str-chat__message-attachment {
    margin: var(--xs-m) 0 var(--xs-m) auto;
  }

  &--me {
    .str-chat__message-attachment {
      padding-left: 0;
    }
  }
}

.str-chat__message-team.thread-list {
  .str-chat__message-attachment {
    --attachment-max-width: 200px;
  }
}

.str-chat__message-attachment {
  overflow: hidden;

  &:hover {
    background: transparent;
  }

  &--card {
    &--no-image {
      height: 60px;
    }

    &--actions {
      height: auto;
    }
  }

  &-file {
    width: 100%;

    &--item {
      position: relative;
      height: 50px;
      display: flex;
      align-items: center;
      font-size: var(--md-font);
      line-height: 22px;
      border-left: 1px solid var(--border);
      width: auto;
      padding-left: var(--xxs-p);

      &:hover {
        background: var(--white);
      }

      .str-chat__message-attachment-file--item-text {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        justify-content: center;
        row-gap: 2px;
        width: 100%;
        min-width: 0;

        .str-chat__message-attachment-file--item-first-row {
          display: flex;
          align-items: center;
          justify-content: space-between;
          column-gap: 8px;
          width: 100%;

          .str-chat__message-attachment-file--item-name {
            font-weight: var(--font-weight-bold);
            color: var(--black);

            white-space: nowrap;
            overflow-y: visible;
            // Not every browser support clip overflow, so hidden is defined as a fallback
            overflow-x: hidden;
            overflow-x: clip;
            text-overflow: ellipsis;
            min-width: 0;
          }

          .str-chat__message-attachment-file--item-download {
            display: flex;

            svg {
              width: 24px;
              height: 16px;

              path {
                fill: var(--black);
              }
            }
          }
        }
      }

      img,
      svg {
        margin-right: var(--xs-m);
      }

      &-text {
        max-width: 100%;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }

      span {
        line-height: 14px;
        font-size: var(--sm-font);
        font-weight: var(--font-weight-bold);
        text-transform: uppercase;
        display: block;
        color: var(--black);
        opacity: 0.5;
      }
    }
  }

  &--image {
    --image-max-height: 300px;
    max-height: var(--image-max-height);
    max-width: var(--attachment-max-width);
    border-radius: 0;

    &:hover {
      background: transparent;
    }

    img {
      @include clamped-height-from-original-image-dimensions(
        '--image-max-height',
        '--attachment-max-width'
      );
      width: auto;
      max-height: inherit;
      display: block;
      object-fit: cover;
      overflow: hidden;

      &:hover {
        background: transparent;
      }
    }

    &--actions {
      height: 320px;

      img {
        height: calc(320px - 40px);
      }
    }
  }

  &--media {
    width: 300px;
  }

  &-card {
    min-height: 60px;
  }
}

.str-chat__message-attachment:not(.str-chat__message-attachment-dynamic-size) {
  .str-chat__message-attachment--image {
    img {
      max-width: 100%;
    }
  }
}

.str-chat__message-attachment-dynamic-size {
  &.str-chat__message-attachment--svg-image {
    img {
      object-fit: contain;
    }
  }

  &.str-chat__message-attachment--image {
    width: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
  }
}

.str-chat__message-attachment-card__giphy-logo {
  height: 20px;
  width: auto;
}

.messaging {
  &.str-chat {
    .str-chat {
      &__message-attachment {
        &.str-chat__message-attachment--image--actions {
          .str-chat__message-attachment--img {
            max-height: 254px;
          }
        }
      }
    }
  }
}

.livestream.str-chat {
  .str-chat {
    &__message-attachment {
      &.str-chat__message-attachment--file {
        max-width: 100%;

        .str-chat__message-attachment-file--item {
          border-left: none;

          &:hover {
            background: var(--grey-whisper);
          }
        }
      }
    }
  }

  &.dark {
    .str-chat {
      &__message-attachment-file--item {
        a,
        span {
          color: var(--white);
        }

        &:hover {
          background: transparent;
        }
      }
    }
  }
}
