import { Box, Input, Tooltip } from '@wix/design-system';
import { FlagSmall, StatusAlertSmall } from '@wix/wix-ui-icons-common';
import React from 'react';
import { FiltersMap } from '@wix/bex-core';
import { CollectionViewsState } from '../../state';
import { observer } from 'mobx-react-lite';

export type ViewsDropdownPrefixProps<T, F extends FiltersMap> = {
  state: CollectionViewsState<T, F>;
};

export function _ViewsDropdownPrefix<T, F extends FiltersMap>(
  props: ViewsDropdownPrefixProps<T, F>,
) {
  const { state } = props;

  const { views } = state;
  const {
    collection: { translate: t },
  } = views;

  if (
    !views.isCurrentViewSelected ||
    (views.isCurrentViewSelected && state.isViewChanged)
  ) {
    return (
      <Input.Affix>
        <Box dataHook="view-pending-changes">
          <Tooltip
            appendTo="window"
            content={
              views.isCurrentViewSelected
                ? t('cairo.manageView.tooltip')
                : t('cairo.manageView.unsaved.tooltip')
            }
          >
            <Box verticalAlign="middle" color="D40">
              <StatusAlertSmall />
            </Box>
          </Tooltip>
        </Box>
      </Input.Affix>
    );
  }

  if (views.isDefaultView) {
    return (
      <Input.Affix>
        <Box dataHook="view-default">
          <FlagSmall />
        </Box>
      </Input.Affix>
    );
  }

  return null;
}

export const ViewsDropdownPrefix = observer(_ViewsDropdownPrefix);
