import OrderBook from "../../../apiwrapper/orderbook/OrderBook.js";
import AbstractConverter from "../AbstractConverter.js";
import ConverterFactory from "../ConverterFactory.js";
import OrderBookEntryConverter from "./OrderBookEntryConverter.js";

export default class OrderBookConverter extends AbstractConverter {

    constructor(){
        super();
    }

    /**
     * @param {OrderBook} obj
     * @returns {object} 
     */
    toRestObject(obj) {
        const obeConverter = new OrderBookEntryConverter();
        return {
            _objectType: ConverterFactory.OBJECT_TYPE_ORDER_BOOK,
            bids: obj.getBids().map(obe => obeConverter.toRestObject(obe)),
            asks: obj.getAsks().map(obe => obeConverter.toRestObject(obe))
        };
    }

    /**
     * @param {object} obj
     * @returns {OrderBook} 
     */
    fromRestObject(obj) {
        const obeConverter = new OrderBookEntryConverter();
        const orderBook = new OrderBook();
        obj.asks.forEach(ask => orderBook.asks.set(ask.price, obeConverter.fromRestObject(ask)));
        obj.bids.forEach(bid => orderBook.bids.set(bid.price, obeConverter.fromRestObject(bid)));

        return orderBook;
    }
}