'use strict';

const index = require('./index-c910887e.js');
const ok = require('./ok-97a5fd0b.js');
const Token = require('./Token-8c5ba5d9.js');
require('./index-9d95c045.js');
const index$1 = require('./index-cc85865d.js');
require('./Item-00ce8c24.js');

exports.Transaction = void 0;
(function (Transaction) {
    function is(value) {
        return (typeof value == "object" &&
            Token.Identifier.is(value.authorization) &&
            typeof value.reference == "string" &&
            index.Merchant.Operation.is(value.type) &&
            (value.card == "debit" || value.card == "credit") &&
            index$1.Card.Scheme.is(value.scheme) &&
            index.Alpha2.is(value.area) &&
            index.Date.is(value.created) &&
            index.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 || index.Date.is(value.reserve.payout)))));
    }
    Transaction.is = is;
    function flaw(value) {
        return {
            type: "Transaction",
            flaws: typeof value != "object"
                ? undefined
                : [
                    Token.Identifier.is(value.authorization) || { property: "authorization", type: "authly.Identifier" },
                    typeof value.reference == "string" || { property: "reference", type: "string" },
                    index.Merchant.Operation.is(value.type) || { property: "type", type: "Merchant.Operation" },
                    ["debit", "credit"].includes(value.card) || { property: "card", type: '"debit" | "credit"' },
                    index$1.Card.Scheme.is(value.scheme) || { property: "scheme", type: "model.Card.Scheme" },
                    index.Alpha2.is(value.area) || { property: "area", type: "isoly.CountryCode.Alpha2" },
                    index.Date.is(value.created) || { property: "created", type: "isoly.Date" },
                    index.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 || index.Date.is(value.reserve.payout))) || {
                        property: "reserve",
                        type: "{amount: number, payout?: isoly.Date}",
                    },
                ].filter(ok.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;
})(exports.Transaction || (exports.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 += `"${index.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 += `"${index.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;
(function (Settlement) {
    function is(value) {
        return (typeof value == "object" &&
            typeof value.reference == "string" &&
            Token.Identifier.is(value.merchant) &&
            typeof value.period == "object" &&
            index.Date.is(value.period.start) &&
            index.Date.is(value.period.end) &&
            (value.payout == undefined || index.Date.is(value.payout)) &&
            (value.reserve == undefined ||
                (typeof value.reserve == "object" &&
                    typeof value.reserve.amount == "number" &&
                    (value.reserve.payout == undefined || index.Date.is(value.reserve.payout)))) &&
            index.Date.is(value.created) &&
            index.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(exports.Transaction.is));
    }
    Settlement.is = is;
    function flaw(value) {
        return {
            type: "Settlement",
            flaws: typeof value != "object"
                ? undefined
                : [
                    typeof value.reference == "string" || { property: "reference", type: "string" },
                    Token.Identifier.is(value.merchant) || { property: "merchant", type: "authly.Identifier" },
                    (typeof value.period == "object" &&
                        index.Date.is(value.period.start) &&
                        index.Date.is(value.period.end)) || {
                        property: "period",
                        type: "{start: isoly.Date, end: isoly.Date}",
                    },
                    value.payout == undefined ||
                        index.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 || index.Date.is(value.reserve.payout))) || {
                        property: "reserve",
                        type: "{amount: number, payout?: isoly.Date}",
                    },
                    index.Date.is(value.created) || { property: "created", type: "isoly.Date" },
                    index.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(exports.Transaction.is) || { property: "transactions", type: "Transaction[]" },
                ].filter(ok.Flaw.is),
        };
    }
    Settlement.flaw = flaw;
    function toCustomer(value) {
        return Array.isArray(value)
            ? value.map(s => toCustomer(s))
            : {
                ...value,
                transactions: exports.Transaction.toCustomer(value.transactions),
                fee: typeof value.fee == "object" ? value.fee.total : value.fee,
            };
    }
    Settlement.toCustomer = toCustomer;
})(Settlement || (Settlement = {}));

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