"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const actions_core_1 = require("@segment/actions-core");
const dd_api_1 = __importDefault(require("../dd-api"));
class DDDataFieldsApi extends dd_api_1.default {
    constructor(settings, client) {
        super(settings, client);
    }
    async getDataFields() {
        const choices = [];
        const response = await this.get('/v2/data-fields/');
        const dataFields = response.data;
        choices.push(...dataFields.map((dataField) => ({
            value: dataField.name,
            label: dataField.name,
            type: this.mapDataFieldType(dataField.type)
        })));
        return { choices };
    }
    mapDataFieldType(fieldType) {
        switch (fieldType) {
            case 'String':
                return 'string';
            case 'Numeric':
                return 'number';
            case 'Date':
                return 'datetime';
            case 'Boolean':
                return 'boolean';
            default:
                throw new actions_core_1.PayloadValidationError(`Invalid data field type: ${fieldType}`);
        }
    }
    isNumeric(value) {
        const type = typeof value;
        return (type === 'number' || type === 'string') && !isNaN(Number(value));
    }
    async validateDataFields(payload) {
        if (!payload.dataFields) {
            return;
        }
        const response = await this.get('/v2/data-fields/');
        const ddDataFields = response.data;
        for (const [key, value] of Object.entries(payload.dataFields)) {
            let validatedValue = value;
            const ddDataField = ddDataFields.find((obj) => obj.name === key);
            if (!ddDataField) {
                throw new actions_core_1.PayloadValidationError(`Data field ${key} not found in Dotdigital`);
            }
            switch (ddDataField.type) {
                case 'Date':
                    if (typeof value !== 'string') {
                        throw new actions_core_1.PayloadValidationError(`Data field ${key} value ${value} is not a valid date`);
                    }
                    else {
                        const date = new Date(value).toISOString();
                        if (date === undefined) {
                            throw new actions_core_1.PayloadValidationError(`Data field ${key} value ${value} is not a valid date`);
                        }
                        validatedValue = date;
                    }
                    break;
                case 'Numeric':
                    if (typeof value === 'string') {
                        if (!this.isNumeric(value)) {
                            throw new actions_core_1.PayloadValidationError(`Data field ${key} value ${value} is not a valid number`);
                        }
                    }
                    else if (typeof value !== 'number') {
                        throw new actions_core_1.PayloadValidationError(`Data field ${key} value ${value} is not a valid number`);
                    }
                    break;
                case 'Boolean':
                    if (typeof value === 'string' && value.trim().toLocaleLowerCase() === 'true') {
                        validatedValue = true;
                    }
                    else if (typeof value === 'string' && value.trim().toLocaleLowerCase() === 'false') {
                        validatedValue = false;
                    }
                    validatedValue = Boolean(validatedValue);
                    break;
                case 'String':
                    if (typeof value !== 'string') {
                        throw new actions_core_1.PayloadValidationError(`Data field ${key} value ${value} is not a valid string`);
                    }
                    validatedValue = String(value).trim();
                    break;
            }
            payload.dataFields[key] = validatedValue;
        }
        return payload.dataFields;
    }
}
exports.default = DDDataFieldsApi;
//# sourceMappingURL=dd-datafields-api.js.map