"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cartFields_1 = require("../fields/cartFields");
const cartLineFields_1 = require("../fields/cartLineFields");
const checkoutFields_1 = require("../fields/checkoutFields");
const collectionFields_1 = require("../fields/collectionFields");
const commonFields_1 = require("../fields/commonFields");
const customerFields_1 = require("../fields/customerFields");
const formFields_1 = require("../fields/formFields");
const productVariantFields_1 = require("../fields/productVariantFields");
const searchFields_1 = require("../fields/searchFields");
const routes_1 = require("../routes");
const transform_payload_1 = require("./transform-payload");
function removeDefaults(fields) {
    return Object.entries(fields).reduce((acc, [key, field]) => {
        const { default: _, ...fieldWithoutDefault } = field;
        return { ...acc, [key]: fieldWithoutDefault };
    }, {});
}
const action = {
    title: 'Save Custom Event',
    description: 'Save a custom event that may have any fields.',
    fields: {
        ...removeDefaults(commonFields_1.commonFields),
        ...removeDefaults(customerFields_1.customerFields),
        ...removeDefaults(cartFields_1.cartFields),
        ...removeDefaults(cartLineFields_1.cartLineFields),
        ...removeDefaults(checkoutFields_1.checkoutFields),
        ...removeDefaults(collectionFields_1.collectionFields),
        ...removeDefaults(formFields_1.formFields),
        ...removeDefaults(productVariantFields_1.productVariantFields),
        ...removeDefaults(searchFields_1.searchFields),
        eventName: {
            label: 'Event Name',
            type: 'string',
            description: 'The name of the event to track.',
            required: true,
            choices: [
                { label: 'page_viewed', value: 'page_viewed' },
                { label: 'cart_viewed', value: 'cart_viewed' },
                { label: 'checkout_address_info_submitted', value: 'checkout_address_info_submitted' },
                { label: 'checkout_completed', value: 'checkout_completed' },
                { label: 'checkout_contact_info_submitted', value: 'checkout_contact_info_submitted' },
                { label: 'checkout_shipping_info_submitted', value: 'checkout_shipping_info_submitted' },
                { label: 'checkout_started', value: 'checkout_started' },
                { label: 'collection_viewed', value: 'collection_viewed' },
                { label: 'payment_info_submitted', value: 'payment_info_submitted' },
                { label: 'product_added_to_cart', value: 'product_added_to_cart' },
                { label: 'product_removed_from_cart', value: 'product_removed_from_cart' },
                { label: 'product_viewed', value: 'product_viewed' },
                { label: 'search_submitted', value: 'search_submitted' },
                { label: 'form_submitted', value: 'form_submitted' },
                { label: 'custom_event', value: 'custom_event' }
            ]
        },
        customEventName: {
            label: 'Custom Event Name',
            type: 'string',
            description: "Additional name for custom events if 'event_name' is 'custom_event'.",
            depends_on: {
                conditions: [
                    {
                        fieldKey: 'eventName',
                        value: 'custom_event',
                        operator: 'is'
                    }
                ]
            }
        }
    },
    perform: (request, data) => {
        const transformedPayload = (0, transform_payload_1.transformPayload)(data.payload);
        const payload = {
            src: 'SEGMENT',
            data: [transformedPayload]
        };
        return request(routes_1.baseURL + (0, routes_1.eventsEndpoint)(data.settings.workspaceId), {
            method: 'post',
            json: payload
        });
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map