import type {Block, Message} from '@modules/chats/interfaces';
import {BlockType} from '@modules/chats/interfaces';

import {
  hasUnsupportedBlocks,
  isMessageForwarded,
  isMessageReplied,
  isSystemMessage,
} from '../blocks';

const createMessage = (blocks?: Block[]): Pick<Message, 'blocks'> => ({
  blocks,
});

describe('blocks utils', () => {
  it('detects system messages only when all blocks are system blocks', () => {
    const systemMessage = createMessage([{type: BlockType.ChannelCreated}]);
    const mixedMessage = createMessage([
      {type: BlockType.ChannelCreated},
      {
        type: BlockType.MessageReplied,
        text: 'reply',
        message_replied_id: 'message-1',
        assets: [],
        blocks: [],
      },
    ]);
    const emptyMessage = createMessage();

    expect(isSystemMessage(systemMessage)).toBe(true);
    expect(isSystemMessage(mixedMessage)).toBe(false);
    expect(isSystemMessage(emptyMessage)).toBe(false);
  });

  it('detects forwarded and replied messages', () => {
    const forwardedMessage = createMessage([
      {type: BlockType.MessageForwarded},
    ]);
    const repliedMessage = createMessage([
      {
        type: BlockType.MessageReplied,
        text: 'reply',
        message_replied_id: 'message-2',
        assets: [],
        blocks: [],
      },
    ]);
    const plainMessage = createMessage([{type: BlockType.ChannelCreated}]);

    expect(isMessageForwarded(forwardedMessage)).toBe(true);
    expect(isMessageForwarded(plainMessage)).toBe(false);
    expect(isMessageReplied(repliedMessage)).toBe(true);
    expect(isMessageReplied(plainMessage)).toBe(false);
  });

  it('returns false when unsupported blocks list is empty', () => {
    const message = createMessage([{type: BlockType.ChannelCreated}]);

    expect(hasUnsupportedBlocks(message)).toBe(false);
  });
});
