import { isNil } from 'lodash';
import { IllegalStateError } from '../../business/errors';
import { ErrorCodes } from '../../business/errors/errorCodes';

export abstract class SerializationClass<ModelType, DTO, ExtraData = Record<string, unknown>> {
    serializeList(serializableList?: ModelType[], extraData?: ExtraData): DTO[] {
        if (isNil(serializableList)) {
            throw new IllegalStateError('The provided list to serialize is invalid', ErrorCodes.ILLEGAL_STATE);
        }
        const results: DTO[] = [];
        serializableList.forEach((serializable: ModelType) => results.push(this.serialize(serializable, extraData)));
        return results;
    }

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