"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const functions_1 = require("../functions");
const properties_1 = require("../properties");
const action = {
    title: 'Remove Profile',
    description: 'Remove profile from list',
    defaultSubscription: 'type = "Identify"',
    fields: {
        email: {
            label: 'Email',
            description: `Individual's email address. One of External ID, or Email required.`,
            type: 'string',
            format: 'email',
            default: { '@path': '$.traits.email' }
        },
        external_id: {
            label: 'External ID',
            description: `A unique identifier used by customers to associate Klaviyo profiles with profiles in an external system. One of External ID, Phone Number and Email required.`,
            type: 'string'
        },
        list_id: {
            label: 'List',
            description: `The Klaviyo list to add the profile to.`,
            type: 'string',
            dynamic: true,
            required: true
        },
        enable_batching: { ...properties_1.enable_batching },
        batch_size: { ...properties_1.batch_size, default: 1000 },
        phone_number: {
            label: 'Phone Number',
            description: `Individual's phone number in E.164 format. If SMS is not enabled and if you use Phone Number as identifier, then you have to provide one of Email or External ID.`,
            type: 'string',
            default: { '@path': '$.traits.phone' }
        },
        country_code: {
            ...properties_1.country_code
        },
        batch_keys: {
            label: 'Batch Keys',
            description: 'The keys to use for batching the events.',
            type: 'string',
            unsafe_hidden: true,
            required: false,
            multiple: true,
            default: ['list_id']
        }
    },
    dynamicFields: {
        list_id: async (request) => {
            return (0, functions_1.getListIdDynamicData)(request);
        }
    },
    perform: async (request, { payload }) => {
        const { email, list_id, external_id, phone_number: initialPhoneNumber, country_code } = payload;
        const phone_number = (0, functions_1.processPhoneNumber)(initialPhoneNumber, country_code);
        if (!email && !external_id && !phone_number) {
            throw new actions_core_1.PayloadValidationError('One of External ID, Phone Number and Email is required.');
        }
        const profileIds = await (0, functions_1.getProfiles)(request, email ? [email] : undefined, external_id ? [external_id] : undefined, phone_number ? [phone_number] : undefined);
        if (!profileIds?.length) {
            throw new actions_core_1.PayloadValidationError('No profiles found for the provided identifiers.');
        }
        return await (0, functions_1.removeProfileFromList)(request, profileIds, list_id);
    },
    performBatch: async (request, { payload, statsContext }) => {
        return await (0, functions_1.removeBulkProfilesFromList)(request, payload, statsContext);
    }
};
exports.default = action;
//# sourceMappingURL=index.js.map