import OrderBook from "../../apiwrapper/orderbook/OrderBook.js";
import OrderBookEntry from "../../apiwrapper/orderbook/OrderBookEntry.js";
import AbstractConverter from "./AbstractConverter.js";
import OrderBookConverter from "./converters/OrderBookConverter.js";
import OrderBookEntryConverter from "./converters/OrderBookEntryConverter.js";

export default class ConverterFactory {
    static OBJECT_TYPE_ORDER_BOOK = 'order_book';
    static OBJECT_TYPE_ORDER_BOOK_ENTRY = 'order_book_entry';

    /**
     * @param {any} obj
     * @returns {AbstractConverter}
     */
    static getConverter(obj) {
        if (obj === null) {
            return null;
        }

        if (typeof obj === 'object' && typeof obj._objectType === 'string') {
            return ConverterFactory.#getConverterByTypeString(obj._objectType);
        }

        if (obj instanceof OrderBook) {
            return new OrderBookConverter();
        } else if (obj instanceof OrderBookEntry) {
            return new OrderBookEntryConverter();
        }

        throw new Error("No converter defined for: " + JSON.stringify(obj));
    }

    /**
     * @param {string} obj
     * @returns {AbstractConverter}
     */
    static #getConverterByTypeString(type) {
        switch (type) {
            case this.OBJECT_TYPE_ORDER_BOOK:
                return new OrderBookConverter();
            case this.OBJECT_TYPE_ORDER_BOOK_ENTRY:
                return new OrderBookEntryConverter();
            default:
                throw new Error("No converter defined for type: " + type);
        }
    }
}