"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateInput = void 0;
const actions_core_1 = require("@segment/actions-core");
const validateInput = (settings, input, event_type) => {
    const properties = {
        api_key: settings.apiKey,
        workspace_key: settings.workspaceIdentifier,
        doc_encoding: 'UTF-8',
        src: 'segment_api',
        screen_resolution: '0',
        account_id: input?.account_id || input?.traits?.companyId || input?.traits?.company?.id,
        ids: {},
        event_type: event_type,
        ...input
    };
    delete properties.event_name;
    if (input?.screen) {
        const { width, height } = input.screen;
        properties.screen_resolution = `${width || 0}x${height || 0}`;
        properties.vp_size = `${width || 0}x${height || 0}`;
        delete properties.screen;
    }
    if (input?.timezone) {
        const offset = new Date()
            .toLocaleString('en-US', { timeZone: input.timezone, timeZoneName: 'short' })
            .split(' ')[3]
            .slice(3);
        properties.local_tz_offset = parseInt(offset) * 60;
        delete properties.timezone;
    }
    if (input?.event_name && event_type === 'track') {
        properties.event_type = input?.event_name;
        delete properties.event_name;
    }
    else {
        properties.event_type = event_type;
    }
    if (event_type === 'user_identify') {
        if (input?.name) {
            properties.traits = {
                email: input?.email,
                name: input?.name,
                created_at: input?.created_at,
                ...properties.traits
            };
        }
        else if (input?.first_name || input?.last_name) {
            properties.traits = {
                email: input?.email,
                name: `${input?.first_name} ${input?.last_name}}`,
                created_at: input?.created_at,
                ...properties.traits
            };
        }
        else {
            throw new actions_core_1.PayloadValidationError('Either name, or first_name and last_name must be provided.');
        }
        if (input?.traits?.company) {
            properties.company = {
                ...input.traits.company
            };
            delete properties.traits.company;
        }
        delete properties.email;
        delete properties.name;
        delete properties.first_name;
        delete properties.last_name;
        delete properties.created_at;
    }
    if (event_type === 'account_identify') {
        properties.traits = {
            name: input?.name,
            created_at: input?.created_at,
            plan_name: input?.plan,
            industry: input?.industry,
            trial_start_date: input?.trial_start,
            trial_expiry_date: input?.trial_end,
            website: input?.website,
            ...properties.traits
        };
        delete properties.name;
        delete properties.created_at;
        delete properties.plan;
        delete properties.industry;
        delete properties.trial_start;
        delete properties.trial_end;
        delete properties.website;
    }
    return properties;
};
exports.validateInput = validateInput;
//# sourceMappingURL=validateInput.js.map