import { JSX } from 'react';
import { LinkPaymentWaitListItem, LinkPaymentWaitListProps } from '../../model/link-pay/types';
import { ListDateGroup } from '../list-date-group';

export const LinkPaymentWaitList = ({
  additionalServiceCategory,
  listItems,
  mid
}: LinkPaymentWaitListProps) => {

  const getListDateGroup = () => {
    let rs = [];
    let date = '';
    let list = [];
    for (let i = 0; i < listItems.length; i++) {
      let items = listItems[i];
      if(!!items) {
        let scheduledSendDate = items?.scheduledSendDate;
        scheduledSendDate = scheduledSendDate?.substring(0, 8);
        if(!!scheduledSendDate) {
          if(i === 0) {
            date = scheduledSendDate;
          }
          if(date !== scheduledSendDate) {
            date = scheduledSendDate;
            if(list.length > 0) {
              rs.push(
                <ListDateGroup
                additionalServiceCategory={additionalServiceCategory}
                key={ date + '-' + i}
                date={ date }
                items={ list }
                ></ListDateGroup>
              );
            }
            list = [];
          }
          list.push(items);
        }
      }
    }
    if (list.length > 0) {
      rs.push(
        <ListDateGroup
          additionalServiceCategory={additionalServiceCategory}
          mid={mid}
          key={date + '-last'}
          date={date}
          items={list}
        ></ListDateGroup>
      );
    }
    return rs;
  };

  return (
    <>
      <div className="transaction-list">
        {getListDateGroup()}
      </div>
    </>
  )
};