import { M as Merchant, A as Alpha2, a as Date, C as Currency } from './index-c2c33860.js';
import { F as Flaw } from './ok-c9833bf3.js';
import { I as Identifier } from './Token-ba2c5d5c.js';
import './index-714bb39b.js';
import { C as Card } from './index-063af7ee.js';
import './Item-f4cbd6f4.js';

var Transaction;
(function (Transaction) {
    function is(value) {
        return (typeof value == "object" &&
            Identifier.is(value.authorization) &&
            typeof value.reference == "string" &&
            Merchant.Operation.is(value.type) &&
            (value.card == "debit" || value.card == "credit") &&
            Card.Scheme.is(value.scheme) &&
            Alpha2.is(value.area) &&
            Date.is(value.created) &&
            Currency.is(value.currency) &&
            typeof value.gross == "number" &&
            (typeof value.fee == "number" ||
                (typeof value.fee == "object" && typeof value.fee.scheme == "number" && typeof value.fee.total == "number")) &&
            typeof value.net == "number" &&
            (value.reserve == undefined ||
                (typeof value.reserve == "object" &&
                    typeof value.reserve.amount == "number" &&
                    (value.reserve.payout == undefined || Date.is(value.reserve.payout)))));
    }
    Transaction.is = is;
    function flaw(value) {
        return {
            type: "Transaction",
            flaws: typeof value != "object"
                ? undefined
                : [
                    Identifier.is(value.authorization) || { property: "authorization", type: "authly.Identifier" },
                    typeof value.reference == "string" || { property: "reference", type: "string" },
                    Merchant.Operation.is(value.type) || { property: "type", type: "Merchant.Operation" },
                    ["debit", "credit"].includes(value.card) || { property: "card", type: '"debit" | "credit"' },
                    Card.Scheme.is(value.scheme) || { property: "scheme", type: "model.Card.Scheme" },
                    Alpha2.is(value.area) || { property: "area", type: "isoly.CountryCode.Alpha2" },
                    Date.is(value.created) || { property: "created", type: "isoly.Date" },
                    Currency.is(value.currency) || { property: "currency", type: "isoly.Currency" },
                    typeof value.gross == "number" || { property: "gross", type: "number" },
                    typeof value.fee == "number" ||
                        (typeof value.fee == "object" &&
                            typeof value.fee.scheme == "number" &&
                            typeof value.fee.total == "number") || {
                        property: "fee",
                        type: "number | { scheme: number; total: number }",
                    },
                    typeof value.net == "number" || { property: "net", type: "number" },
                    value.reserve == undefined ||
                        (typeof value.reserve == "object" &&
                            typeof value.reserve.amount == "number" &&
                            (value.reserve.payout == undefined || Date.is(value.reserve.payout))) || {
                        property: "reserve",
                        type: "{amount: number, payout?: isoly.Date}",
                    },
                ].filter(Flaw.is),
        };
    }
    Transaction.flaw = flaw;
    function toCsv(transactions) {
        var _a, _b;
        let result = "authorization,reference,type,card,scheme,area,created,currency,gross,fee,net,reserve amount,reserve payout\r\n";
        for (const value of transactions)
            result += `"${value.authorization}","${value.reference}","${value.type}","${value.card}","${value.scheme}","${value.area}","${value.created}","${value.currency}","${value.gross}","${typeof value.fee == "number" ? value.fee : value.fee.total}","${value.net}","${(_a = value.reserve) === null || _a === void 0 ? void 0 : _a.amount}","${(_b = value.reserve) === null || _b === void 0 ? void 0 : _b.payout}"\r\n`;
        return result;
    }
    Transaction.toCsv = toCsv;
    function toCustomer(value) {
        return Array.isArray(value)
            ? value.map(t => toCustomer(t))
            : { ...value, fee: typeof value.fee == "object" ? value.fee.total : value.fee };
    }
    Transaction.toCustomer = toCustomer;
})(Transaction || (Transaction = {}));

var Conversion;
(function (Conversion) {
    function toCsv(settlements) {
        var _a, _b, _c, _d, _e, _f, _g;
        let result = "";
        result += "reference,";
        result += "merchant,";
        result += "start,";
        result += "end,";
        result += "payout date,";
        result += "payout amount,";
        result += "reserve release,";
        result += "reserve amount,";
        result += "created,";
        result += "gross,";
        result += "fee,";
        result += "interchange,";
        result += "net,";
        result += "currency,";
        result += "transactions\r\n";
        for (const settlement of settlements) {
            result += `"${settlement.reference}",`;
            result += `"${settlement.merchant}",`;
            result += `"${settlement.period.start}",`;
            result += `"${settlement.period.end}",`;
            result += `"${(_a = settlement.payout) !== null && _a !== void 0 ? _a : ""}",`;
            result += `"${Currency.round(settlement.net - ((_c = (_b = settlement.reserve) === null || _b === void 0 ? void 0 : _b.amount) !== null && _c !== void 0 ? _c : 0), settlement.currency)}",`;
            result += `"${(_e = (_d = settlement.reserve) === null || _d === void 0 ? void 0 : _d.payout) !== null && _e !== void 0 ? _e : ""}",`;
            result += `"${(_g = (_f = settlement.reserve) === null || _f === void 0 ? void 0 : _f.amount) !== null && _g !== void 0 ? _g : 0}",`;
            result += `"${settlement.created}",`;
            result += `"${settlement.gross}",`;
            result += `"${typeof settlement.fee == "number" ? settlement.fee : settlement.fee.total}",`;
            result += `"${typeof settlement.fee == "number" ? 0 : settlement.fee.scheme}",`;
            result += `"${settlement.net}",`;
            result += `"${settlement.currency}",`;
            result += `"${settlement.transactions.length}"`;
            result += `\r\n`;
        }
        return result;
    }
    Conversion.toCsv = toCsv;
    function toDetailedCsv(settlements, authorizations) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j;
        let result = "";
        result += "merchant,";
        result += "settlement reference,";
        result += "operation,";
        result += "posting date,";
        result += "authorization id,";
        result += "authorization number,";
        result += "card IIN,";
        result += "card type,";
        result += "card scheme,";
        result += "scheme reference,";
        result += "issuer country,";
        result += "currency,";
        result += "payout date,";
        result += "payout amount,";
        result += "reserve release,";
        result += "reserve amount,";
        result += "gross,";
        result += "fee,";
        result += "interchange,";
        result += "net";
        result += "\r\n";
        for (const settlement of settlements) {
            for (const transaction of settlement.transactions) {
                const authorization = authorizations.find(a => transaction.authorization == a.id);
                result += `"${settlement.merchant}",`;
                result += `"${settlement.reference}",`;
                result += `"${transaction.type}",`;
                result += `"${transaction.created}",`;
                result += `"${transaction.authorization}",`;
                result += `"${(_a = authorization === null || authorization === void 0 ? void 0 : authorization.number) !== null && _a !== void 0 ? _a : ""}",`;
                result += `"${(_b = authorization === null || authorization === void 0 ? void 0 : authorization.card.iin) !== null && _b !== void 0 ? _b : ""}",`;
                result += `"${transaction.card}",`;
                result += `"${transaction.scheme}",`;
                result += `"${transaction.reference}",`;
                result += `"${transaction.area}",`;
                result += `"${transaction.currency}",`;
                result += `"${(_c = settlement === null || settlement === void 0 ? void 0 : settlement.payout) !== null && _c !== void 0 ? _c : ""}",`;
                result += `"${Currency.round(transaction.net - ((_e = (_d = transaction.reserve) === null || _d === void 0 ? void 0 : _d.amount) !== null && _e !== void 0 ? _e : 0), transaction.currency)}",`;
                result += `"${(_g = (_f = transaction.reserve) === null || _f === void 0 ? void 0 : _f.payout) !== null && _g !== void 0 ? _g : ""}",`;
                result += `"${(_j = (_h = transaction.reserve) === null || _h === void 0 ? void 0 : _h.amount) !== null && _j !== void 0 ? _j : 0}",`;
                result += `"${transaction.gross}",`;
                result += `"${typeof transaction.fee == "number" ? transaction.fee : transaction.fee.total}",`;
                result += `"${typeof transaction.fee == "number" ? 0 : transaction.fee.scheme}",`;
                result += `"${transaction.net}"`;
                result += `\r\n`;
            }
        }
        return result;
    }
    Conversion.toDetailedCsv = toDetailedCsv;
})(Conversion || (Conversion = {}));

var Settlement$1;
(function (Settlement) {
    function is(value) {
        return (typeof value == "object" &&
            typeof value.reference == "string" &&
            Identifier.is(value.merchant) &&
            typeof value.period == "object" &&
            Date.is(value.period.start) &&
            Date.is(value.period.end) &&
            (value.payout == undefined || Date.is(value.payout)) &&
            (value.reserve == undefined ||
                (typeof value.reserve == "object" &&
                    typeof value.reserve.amount == "number" &&
                    (value.reserve.payout == undefined || Date.is(value.reserve.payout)))) &&
            Date.is(value.created) &&
            Currency.is(value.currency) &&
            typeof value.gross == "number" &&
            (typeof value.fee == "number" ||
                (typeof value.fee == "object" && typeof value.fee.scheme == "number" && typeof value.fee.total == "number")) &&
            typeof value.net == "number" &&
            Array.isArray(value.transactions) &&
            value.transactions.every(Transaction.is));
    }
    Settlement.is = is;
    function flaw(value) {
        return {
            type: "Settlement",
            flaws: typeof value != "object"
                ? undefined
                : [
                    typeof value.reference == "string" || { property: "reference", type: "string" },
                    Identifier.is(value.merchant) || { property: "merchant", type: "authly.Identifier" },
                    (typeof value.period == "object" &&
                        Date.is(value.period.start) &&
                        Date.is(value.period.end)) || {
                        property: "period",
                        type: "{start: isoly.Date, end: isoly.Date}",
                    },
                    value.payout == undefined ||
                        Date.is(value.payout) || { property: "payout", type: "isoly.Date | undefined" },
                    value.reserve == undefined ||
                        (typeof value.reserve == "object" &&
                            typeof value.reserve.amount == "number" &&
                            (value.reserve.payout == undefined || Date.is(value.reserve.payout))) || {
                        property: "reserve",
                        type: "{amount: number, payout?: isoly.Date}",
                    },
                    Date.is(value.created) || { property: "created", type: "isoly.Date" },
                    Currency.is(value.currency) || { property: "currency", type: "isoly.Currency" },
                    typeof value.gross == "number" || { property: "gross", type: "number" },
                    typeof value.fee == "number" ||
                        (typeof value.fee == "object" &&
                            typeof value.fee.scheme == "number" &&
                            typeof value.fee.total == "number") || {
                        property: "fee",
                        type: "number | { scheme: number; total: number }",
                    },
                    typeof value.net == "number" || { property: "net", type: "number" },
                    value.transactions.every(Transaction.is) || { property: "transactions", type: "Transaction[]" },
                ].filter(Flaw.is),
        };
    }
    Settlement.flaw = flaw;
    function toCustomer(value) {
        return Array.isArray(value)
            ? value.map(s => toCustomer(s))
            : {
                ...value,
                transactions: Transaction.toCustomer(value.transactions),
                fee: typeof value.fee == "object" ? value.fee.total : value.fee,
            };
    }
    Settlement.toCustomer = toCustomer;
})(Settlement$1 || (Settlement$1 = {}));

var Settlement;
(function (Settlement) {
    Settlement.toCsv = Conversion.toCsv;
    Settlement.toDetailedCsv = Conversion.toDetailedCsv;
    Settlement.is = Settlement$1.is;
    Settlement.flaw = Settlement$1.flaw;
    Settlement.toCustomer = Settlement$1.toCustomer;
    (function (Transaction$1) {
        Transaction$1.is = Transaction.is;
        Transaction$1.flaw = Transaction.flaw;
        Transaction$1.toCustomer = Transaction.toCustomer;
    })(Settlement.Transaction || (Settlement.Transaction = {}));
})(Settlement || (Settlement = {}));

export { Settlement as S, Transaction as T };
