import { Button } from 'antd';
import { DeleteOutlined, EditOutlined } from '@ant-design/icons';
import { showConfirmModal } from '../../components/Modals/ConfirmModal/ConfirmModal';
import { assetsTypeValue } from './MOCK';

export const getCards = (
  assetsList: any,
  tagsList: any,
  currentLeftSideBarTab: any,
  ctActions: (props: any) => void,
  question: string,
  okText: string,
  cancelText: string,
): ReactNodes => {
  const cta = (e: any, id: string, action: string): void => {
    e.stopPropagation();
    if (action === 'edit') {
      ctActions({ action: 'edit', id });
    }
    if (action === 'delete')
      showConfirmModal({
        title: question,
        okType: 'danger',
        action: () => ctActions({ action: 'delete', id: [id] }),
        okText: okText,
        cancelText: cancelText,
      });
  };

  const checkTag = (tag: any): string => {
    const currentGroup = assetsList
      .filter((item: any) => item.tags?.id === tag.id)
      .map((asset: any) => {
        return <div key={tag.id}>{asset.name}</div>;
      });

    return currentGroup.length ? currentGroup : '--------';
  };

  switch (currentLeftSideBarTab) {
    case 'assets':
      return assetsList.map((asset: any) => {
        return {
          ...asset,
          asset_type: assetsTypeValue.find(
            (item) => asset.asset_type === item.value,
          )?.title,
        };
      });

    case 'groups': {
      return tagsList.map((tag: any) => {
        return {
          ...tag,
          key: tag.id,
          group: checkTag(tag),

          actions: (
            <>
              <Button
                onClick={(e) => {
                  cta(e, tag.id, 'edit');
                }}
                style={{ padding: '4px 5px' }}
                type="link"
              >
                <EditOutlined />
              </Button>
              <Button
                onClick={(e) => cta(e, tag.id, 'delete')}
                style={{ padding: '4px 5px' }}
                type="link"
              >
                <DeleteOutlined />
              </Button>
            </>
          ),
        };
      });
    }
  }
};
