import { css } from '@emotion/react';
import styled from '@emotion/styled';

export const EditableFieldNormalModeOuterContainer = styled.div<
  Pick<
    OwnProps,
    | 'disableClick'
    | 'isDisplayModeContentEmpty'
    | 'disableHoverEffect'
    | 'isDisplayModeFixHeight'
  >
>`
  align-items: center;
  border-radius: ${({ theme }) => theme.border.radius.sm};
  display: flex;
  height: ${({ isDisplayModeFixHeight }) =>
    isDisplayModeFixHeight ? '16px' : 'auto'};
  min-height: 16px;

  overflow: hidden;

  padding: ${({ theme }) => theme.spacing(1)};

  ${(props) => {
    if (props.isDisplayModeContentEmpty) {
      return css`
        min-width: 50px;
      `;
    } else {
      return css`
        width: fit-content;
      `;
    }
  }}

  ${(props) => {
    if (props.disableClick) {
      return css`
        cursor: default;
      `;
    } else {
      return css`
        cursor: pointer;

        &:hover {
          background-color: ${!props.disableHoverEffect &&
          props.theme.background.transparent.light};
        }
      `;
    }
  }}
`;

export const EditableFieldNormalModeInnerContainer = styled.div`
  align-items: center;
  color: ${({ theme }) => theme.font.color.primary};
  font-size: 'inherit';
  font-weight: 'inherit';

  height: fit-content;

  overflow: hidden;

  text-overflow: ellipsis;
  white-space: nowrap;
`;

type OwnProps = {
  disableClick?: boolean;
  onClick?: () => void;
  isDisplayModeContentEmpty?: boolean;
  disableHoverEffect?: boolean;
  isDisplayModeFixHeight?: boolean;
};

export function EditableFieldDisplayMode({
  children,
  disableClick,
  onClick,
  isDisplayModeContentEmpty,
  disableHoverEffect,
  isDisplayModeFixHeight,
}: React.PropsWithChildren<OwnProps>) {
  return (
    <EditableFieldNormalModeOuterContainer
      onClick={disableClick ? undefined : onClick}
      disableClick={disableClick}
      isDisplayModeContentEmpty={isDisplayModeContentEmpty}
      disableHoverEffect={disableHoverEffect}
      isDisplayModeFixHeight={isDisplayModeFixHeight}
    >
      <EditableFieldNormalModeInnerContainer>
        {children}
      </EditableFieldNormalModeInnerContainer>
    </EditableFieldNormalModeOuterContainer>
  );
}
