import { formatCurrency } from "../utils/utils";
import { DisputeStatus } from "./Dispute";
export var CaptureStrategy;
(function (CaptureStrategy) {
    CaptureStrategy["automatic"] = "automatic";
    CaptureStrategy["manual"] = "manual";
})(CaptureStrategy || (CaptureStrategy = {}));
export var PaymentMethodTypes;
(function (PaymentMethodTypes) {
    PaymentMethodTypes["card"] = "card";
    PaymentMethodTypes["bankAccount"] = "bankAccount";
    PaymentMethodTypes["sezzle"] = "sezzle";
    PaymentMethodTypes["plaid"] = "plaid";
    PaymentMethodTypes["applePay"] = "applePay";
})(PaymentMethodTypes || (PaymentMethodTypes = {}));
export var PaymentTypes;
(function (PaymentTypes) {
    PaymentTypes["card"] = "Card";
    PaymentTypes["bankAccount"] = "ACH";
    PaymentTypes["unknown"] = "Unknown";
})(PaymentTypes || (PaymentTypes = {}));
export var PaymentStatuses;
(function (PaymentStatuses) {
    PaymentStatuses["pending"] = "pending";
    PaymentStatuses["automatic"] = "automatic";
    PaymentStatuses["authorized"] = "authorized";
    PaymentStatuses["succeeded"] = "succeeded";
    PaymentStatuses["failed"] = "failed";
    PaymentStatuses["canceled"] = "canceled";
    PaymentStatuses["disputed"] = "disputed";
    PaymentStatuses["fully_refunded"] = "fully_refunded";
    PaymentStatuses["partially_refunded"] = "partially_refunded";
})(PaymentStatuses || (PaymentStatuses = {}));
export var CurrencyTypes;
(function (CurrencyTypes) {
    CurrencyTypes["usd"] = "usd";
    CurrencyTypes["cad"] = "cad";
})(CurrencyTypes || (CurrencyTypes = {}));
export class PaymentMethod {
    constructor(paymentMethod) {
        this.card = paymentMethod.card ? new Card(paymentMethod.card) : undefined;
        this.bank_account = paymentMethod.bank_account
            ? new BankAccount(paymentMethod.bank_account)
            : undefined;
    }
    get payersName() {
        if (this.card) {
            return this.card.name;
        }
        else if (this.bank_account) {
            return this.bank_account.name;
        }
        return null;
    }
    get lastFourDigits() {
        if (this.card) {
            return `**** ${this.card.acct_last_four}`;
        }
        else if (this.bank_account) {
            return `**** ${this.bank_account.acct_last_four}`;
        }
        return null;
    }
}
export class BankAccount {
    constructor(bankAccount) {
        this.id = bankAccount.id;
        this.acct_last_four = bankAccount.acct_last_four;
        this.name = bankAccount.name;
        this.brand = bankAccount.brand;
        this.token = bankAccount.token;
        this.created_at = bankAccount.created_at;
        this.updated_at = bankAccount.updated_at;
    }
}
export class Card {
    constructor(card) {
        this.id = card.id || '';
        this.acct_last_four = card.acct_last_four;
        this.name = card.name;
        this.brand = card.brand;
        this.token = card.token;
        this.created_at = card.created_at;
        this.updated_at = card.updated_at;
    }
}
export class Payment {
    constructor(payment) {
        this.id = payment.id;
        this.account_id = payment.account_id;
        this.currency = payment.currency;
        this.amount = payment.amount;
        this.amount_disputed = payment.amount_disputed;
        this.amount_refundable = payment.amount_refundable;
        this.amount_refunded = payment.amount_refunded;
        this.balance = payment.balance;
        this.captured = payment.captured;
        this.capture_strategy = payment.capture_strategy;
        this.description = payment.description;
        this.disputed = payment.disputed;
        this.disputes = payment.disputes;
        this.error_code = payment.error_code;
        this.error_description = payment.error_description;
        this.expedited = payment.expedited;
        this.fee_amount = payment.fee_amount;
        this.is_test = payment.is_test;
        this.metadata = payment.metadata;
        this.payment_method = new PaymentMethod(payment.payment_method);
        this.payment_intent_id = payment.payment_intent_id;
        this.refunded = payment.refunded;
        this.status = payment.status;
        this.created_at = payment.created_at;
        this.updated_at = payment.updated_at;
    }
    get disputedStatus() {
        const lost = this.disputes.some((dispute) => dispute.status === DisputeStatus.lost);
        // if a dispute is 'won', we don't show a dispute status, just general status
        // TODO: update this logic to work with new DisputeStatus enum
        // (cast 'open' as DisputeStatus in the meantime to keep existing functionality)
        if (!this.disputed) {
            return null;
        }
        else if (lost) {
            return DisputeStatus.lost;
        }
        else {
            return 'open';
        }
    }
    get payment_type() {
        if (this.payment_method) {
            return this.payment_method.card
                ? PaymentTypes.card
                : PaymentTypes.bankAccount;
        }
        else {
            return PaymentTypes.unknown;
        }
    }
    get payers_name() {
        return this.payment_method.payersName;
    }
    get last_four_digits() {
        return this.payment_method.lastFourDigits;
    }
    formattedPaymentAmount(amount) {
        return formatCurrency(amount, this.currency);
    }
}
