import { _LinksDTO } from './_linksDTO';
import { SerializationClass } from './serializationClass';

export abstract class CursorPaginatedSerializationClass<ModelType, SerializedType, ExtraData = Record<string, unknown>> extends SerializationClass<
    ModelType,
    SerializedType,
    ExtraData
> {
    public serializeCursorPaginatedList = (
        rows: ModelType[],
        cursor: string | null,
        options?: { count?: number | null; extraData?: ExtraData },
    ): CursorPaginatedResponse<SerializedType> => {
        const response: CursorPaginatedResponse<SerializedType> = {
            cursor,
            items: super.serializeList(rows, options?.extraData),
        };
        if (options?.count !== undefined) {
            response.count = options.count;
        }
        return response;
    };

    public serializeCursorPaginatedListWithLinks = (
        rows: ModelType[],
        cursor: string | null,
        extraData?: ExtraData,
        _links?: _LinksDTO,
    ): CursorPaginatedResponseWithLinks<SerializedType> => {
        return {
            _links,
            cursor,
            items: super.serializeList(rows, extraData),
        };
    };

    abstract serialize(serializableObject: ModelType, extraData?: ExtraData): SerializedType;
}

export type CursorPaginatedResponse<SerializedType> = {
    count?: number | null;
    cursor: string | null;
    items: SerializedType[];
};

export type CursorPaginatedResponseWithLinks<SerializedType> = {
    cursor: string | null;
    items: SerializedType[];
    _links?: _LinksDTO;
};
