import _defineProperty from "@babel/runtime/helpers/defineProperty";
/** @jsx jsx */
import React, { Fragment } from 'react';
import { css, jsx } from '@emotion/react';
// AFP-2532 TODO: Fix automatic suppressions below
// eslint-disable-next-line @atlassian/tangerine/import/entry-points

import { injectIntl } from 'react-intl-next';
import { relativeFontSizeToBase16 } from '@atlaskit/editor-shared-styles';
import { N20, N300, N800 } from '@atlaskit/theme/colors';
import { fontSizeSmall } from '@atlaskit/theme/constants';
import { getCorrectAltByIconUrl } from './listItemAlts';
import { transformTimeStamp } from './transformTimeStamp';
export const container = css({
  backgroundColor: 'transparent',
  padding: `${"var(--ds-space-100, 8px)"} ${"var(--ds-space-150, 12px)"}`,
  cursor: 'pointer',
  display: 'flex',
  marginTop: 0
});
export const containerSelected = css({
  backgroundColor: `var(--ds-background-neutral-subtle-hovered, ${N20})`
});
const nameWrapper = css({
  overflow: 'hidden'
});
export const nameStyle = css({
  color: `var(--ds-text, ${N800})`,
  overflow: 'hidden',
  textOverflow: 'ellipsis',
  whiteSpace: 'nowrap',
  lineHeight: '20px'
});
export const containerName = css({
  color: `var(--ds-text-subtlest, ${N300})`,
  lineHeight: '14px',
  fontSize: relativeFontSizeToBase16(fontSizeSmall())
});
const iconStyle = css({
  minWidth: '16px',
  marginTop: "var(--ds-space-050, 4px)",
  marginRight: "var(--ds-space-150, 12px)",
  img: {
    maxWidth: '16px'
  }
});
class LinkSearchListItem extends React.PureComponent {
  constructor(...args) {
    super(...args);
    _defineProperty(this, "handleSelect", e => {
      e.preventDefault(); // don't let editor lose focus
      const {
        item,
        onSelect
      } = this.props;
      onSelect(item.url, item.name);
    });
    _defineProperty(this, "handleMouseMove", () => {
      const {
        onMouseMove,
        item
      } = this.props;
      onMouseMove && onMouseMove(item.objectId);
    });
    _defineProperty(this, "handleMouseEnter", () => {
      const {
        onMouseEnter,
        item
      } = this.props;
      onMouseEnter && onMouseEnter(item.objectId);
    });
    _defineProperty(this, "handleMouseLeave", () => {
      const {
        onMouseLeave,
        item
      } = this.props;
      onMouseLeave && onMouseLeave(item.objectId);
    });
  }
  renderIcon() {
    const {
      item: {
        icon,
        iconUrl
      },
      intl
    } = this.props;
    if (icon) {
      return jsx("span", {
        css: iconStyle
      }, icon);
    }
    if (iconUrl) {
      return jsx("span", {
        css: iconStyle
      }, jsx("img", {
        src: iconUrl,
        alt: getCorrectAltByIconUrl(iconUrl, intl)
      }));
    }
    return null;
  }
  renderTimeStamp() {
    const {
      item,
      intl
    } = this.props;
    const date = transformTimeStamp(intl, item.lastViewedDate, item.lastUpdatedDate);
    return date && jsx(Fragment, null, "\xA0 \u2022", jsx("span", {
      className: "link-search-timestamp",
      "data-test-id": "link-search-timestamp"
    }, "\xA0 ", date.pageAction, " ", date.dateString, " ", date.timeSince || ''));
  }
  render() {
    const {
      item,
      selected,
      id,
      role
    } = this.props;
    return jsx("li", {
      css: [container, selected && containerSelected],
      role: role,
      id: id,
      "aria-selected": selected,
      "data-testid": "link-search-list-item",
      onMouseMove: this.handleMouseMove,
      onMouseEnter: this.handleMouseEnter,
      onMouseLeave: this.handleMouseLeave,
      onClick: this.handleSelect
    }, this.renderIcon(), jsx("span", {
      css: nameWrapper
    }, jsx("div", {
      css: nameStyle
    }, item.name), jsx("div", {
      "data-testid": "link-search-list-item-container",
      css: containerName
    }, item.container, this.renderTimeStamp())));
  }
}
export default injectIntl(LinkSearchListItem);