"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const index_1 = require("../index");
function formatPurchasePayload(purchaseEvent) {
    purchaseEvent.purchase_datetime = (0, index_1.formatTimestampAsUnixSeconds)(purchaseEvent.purchase_datetime);
    const codeList = [];
    if (purchaseEvent.discount_code) {
        codeList.push(purchaseEvent.discount_code);
    }
    if (purchaseEvent.products) {
        for (const product of purchaseEvent.products) {
            if (product.coupon) {
                codeList.push(product.coupon);
            }
        }
    }
    const productList = [];
    if (purchaseEvent.products) {
        for (const product of purchaseEvent.products) {
            const productIdentifier = product.product_id || product.sku;
            if (productIdentifier) {
                productList.push(productIdentifier);
            }
        }
    }
    return {
        ...(0, actions_core_1.omit)(purchaseEvent, ["products", "discount_code"]),
        "product_column": productList.join("|"),
        "discount_code": codeList.join("|")
    };
}
const action = {
    title: 'Send Transaction Event',
    description: 'Send a purchase event to Actable for prediction. Purchase events should be in v2 Commerce Spec.',
    fields: {
        customer_id: {
            label: 'Customer ID',
            description: 'The unique user identifier.',
            type: 'string',
            required: true,
            default: {
                '@path': '$.userId'
            }
        },
        discount_code: {
            type: "string",
            label: "Discount Code",
            description: "Discount code, if any, used on purchase. Will be used in addition to per-product coupons in Segment v2commerce events spec.",
            required: false
        },
        transaction_id: {
            type: "string",
            label: "Transaction ID",
            description: "Optional Identifier for transaction.",
            required: false,
            default: { '@path': '$.properties.order_id' }
        },
        spend: {
            type: "number",
            label: "Amount",
            description: "Total order amount.",
            required: false,
            default: { '@path': '$.properties.total' }
        },
        products: {
            type: "object",
            multiple: true,
            label: "Products Purchased",
            description: "product(s) purchased in transaction. This value should be an array of objects which at the minimum contains a Product ID or SKU per-product.",
            required: true,
            default: { '@path': '$.properties.products' }
        },
        purchase_datetime: {
            type: "datetime",
            label: "Timestamp",
            description: "timestamp of when transaction event occurred.",
            required: true,
            default: { '@path': '$.timestamp' }
        },
        stream_key: {
            type: "string",
            label: "Stream Key",
            description: "Dataset label, should be left as default unless directed otherwise.",
            required: true,
            default: "transaction"
        }
    },
    perform: (request, data) => {
        return request(index_1.API_URL, {
            method: 'post',
            json: { data: [formatPurchasePayload(data.payload)] }
        });
    },
    performBatch: (request, data) => {
        const formattedPayload = [];
        for (const ev of data.payload) {
            formattedPayload.push(formatPurchasePayload(ev));
        }
        return request(index_1.API_URL, {
            method: 'post',
            json: { data: formattedPayload }
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map