import TradingPairs from "../../../apiwrapper/assets/TraidingPairs.js";
import Transaction from "../../../apiwrapper/transaction/Transaction.js";
import AbstractConverter from "../AbstractConverter.js";
import ConverterFactory from "../ConverterFactory.js";
import TransactionSettingsConverter from "./TransactionSettingsConverter.js";

export default class TransactionConverter extends AbstractConverter {

    constructor(){
        super();
    }

    /**
     * @param {Transaction} obj
     * @returns {object} 
     */
    toRestObject(obj) {
        const tsConverter = new TransactionSettingsConverter();

        return {
            _objectType: ConverterFactory.OBJECT_TYPE_TRANSACTION,
            id: obj.id,
            initiator: obj.initiator,
            phase: obj.phase,
            timestamp:obj.timestamp,
            result: obj.result,
            buySettings: obj.buySettings == null ? null : tsConverter.toRestObject(obj.buySettings),
            sellSettings: obj.sellSettings == null ? null : tsConverter.toRestObject(obj.sellSettings),
            symbol: obj.symbol.getKey()
        };
    }

    /**
     * @param {object} obj
     * @returns {Transaction} 
     */
    fromRestObject(obj) {
        const t = new Transaction(null, null);
        const tsConverter = new TransactionSettingsConverter();

        t.id = obj.id;
        t.initiator = obj.initiator;
        t.phase = obj.phase;
        t.timestamp = obj.timestamp;
        t.result = obj.result;
        t.buySettings = obj.buySettings == null ? null : tsConverter.fromRestObject(obj.buySettings);
        t.sellSettings = obj.sellSettings == null ? null : tsConverter.fromRestObject(obj.sellSettings);
        t.symbol = TradingPairs.fromString(obj.symbol);

        return t;
    }
}