import { formatCurrency } from "../utils/utils";
export class Completion {
    constructor(data) {
        this.payment_mode = ICheckoutPaymentMode[data.payment_mode];
        this.payment_token = data.payment_token;
        this.payment_status = data.payment_status;
        this.payment_response = data.payment_response;
        this.payment_error_code = data.payment_error_code;
        this.payment_error_description = data.payment_error_description;
        this.checkout_id = data.checkout_id;
        this.additional_transactions = data.additional_transactions;
        this.payment_id = data.payment_id;
        this.payment_method_id = data.payment_method_id;
        this.status = data.status;
        this.created_at = data.created_at;
        this.updated_at = data.updated_at;
    }
}
export class Checkout {
    constructor(data) {
        var _a;
        this.id = data.id;
        this.description = data.description;
        this.payment_intent_id = data.payment_intent_id;
        this.account_id = data.account_id;
        this.platform_account_id = data.platform_account_id;
        this.payment_amount = data.payment_amount;
        this.payment_client_id = data.payment_client_id;
        this.payment_description = data.payment_description;
        this.payment_methods = data.payment_methods;
        this.payment_method_group_id = data.payment_method_group_id;
        this.payment_settings = data.payment_settings;
        this.bnpl = data.bnpl;
        this.total_amount = data.total_amount;
        this.insurance_amount = data.insurance_amount;
        this.status = ICheckoutStatus[data.status];
        this.payment_currency = data.payment_currency;
        this.mode = data.mode;
        this.statement_descriptor = data.statement_descriptor;
        this.application_fees = data.application_fees;
        this.successful_payment_id = data.successful_payment_id;
        this.created_at = data.created_at;
        this.updated_at = data.updated_at;
        this.completions = (_a = data.completions) === null || _a === void 0 ? void 0 : _a.map((completion) => new Completion(completion));
    }
    get payment_mode() {
        var _a, _b, _c, _d, _e, _f;
        let paymentMode;
        if (!this.completions || this.completions.length === 0) {
            return ICheckoutPaymentMode.unknown;
        }
        const sortedCompletions = this.completions.sort((a, b) => {
            return (new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime());
        });
        const lastCompletion = sortedCompletions[0];
        if (lastCompletion.payment_mode) {
            paymentMode = lastCompletion.payment_mode;
        }
        else if ((_c = (_b = (_a = lastCompletion.payment_response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.payment_method) === null || _c === void 0 ? void 0 : _c.card) {
            paymentMode = ICheckoutPaymentMode.card;
        }
        else if ((_f = (_e = (_d = lastCompletion.payment_response) === null || _d === void 0 ? void 0 : _d.data) === null || _e === void 0 ? void 0 : _e.payment_method) === null || _f === void 0 ? void 0 : _f.bank_account) {
            paymentMode = ICheckoutPaymentMode.bank_account;
        }
        return paymentMode;
    }
    formattedPaymentAmount(amount) {
        return formatCurrency(amount, this.payment_currency);
    }
}
export var ICheckoutPaymentMode;
(function (ICheckoutPaymentMode) {
    ICheckoutPaymentMode["bnpl"] = "Buy Now Pay Later";
    ICheckoutPaymentMode["ecom"] = "E-commerce";
    ICheckoutPaymentMode["card"] = "Card";
    ICheckoutPaymentMode["bank_account"] = "Bank Account";
    ICheckoutPaymentMode["card_present"] = "Card Present";
    ICheckoutPaymentMode["apple_pay"] = "Apple Pay";
    ICheckoutPaymentMode["unknown"] = "";
})(ICheckoutPaymentMode || (ICheckoutPaymentMode = {}));
export var ICheckoutPaymentModeParam;
(function (ICheckoutPaymentModeParam) {
    ICheckoutPaymentModeParam["bnpl"] = "bnpl";
    ICheckoutPaymentModeParam["ecom"] = "ecom";
    ICheckoutPaymentModeParam["apple_pay"] = "apple_pay";
})(ICheckoutPaymentModeParam || (ICheckoutPaymentModeParam = {}));
export var ICheckoutStatus;
(function (ICheckoutStatus) {
    ICheckoutStatus["created"] = "created";
    ICheckoutStatus["completed"] = "completed";
    ICheckoutStatus["attempted"] = "attempted";
    ICheckoutStatus["expired"] = "expired";
})(ICheckoutStatus || (ICheckoutStatus = {}));
export var CompletionStatuses;
(function (CompletionStatuses) {
    CompletionStatuses["failed"] = "failed";
    CompletionStatuses["succeeded"] = "succeeded";
})(CompletionStatuses || (CompletionStatuses = {}));
