"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSchemaFromHubspot = getSchemaFromHubspot;
exports.createProperties = createProperties;
const actions_core_1 = require("@segment/actions-core");
const schema_functions_1 = require("./schema-functions");
const types_1 = require("../types");
async function getSchemaFromHubspot(client, schema) {
    const requests = [];
    const hasProps = schema.properties.length;
    const hasSensitiveProps = schema.sensitiveProperties.length;
    const hsSchema = {
        object_details: schema.object_details,
        properties: [],
        sensitiveProperties: []
    };
    if (hasProps) {
        requests.push(client.readProperties(false));
    }
    if (hasSensitiveProps) {
        requests.push(client.readProperties(true));
    }
    const responses = await Promise.all(requests);
    if (hasProps && requests.length > 0) {
        const response = responses.shift();
        hsSchema.properties = extractProperties(response);
    }
    if (hasSensitiveProps && requests.length > 0) {
        const response = responses.shift();
        hsSchema.sensitiveProperties = extractProperties(response);
    }
    return hsSchema;
}
function extractProperties(response) {
    const results = response.data.results ?? [];
    const props = [];
    results.forEach((item) => {
        const hSPropTypeFieldType = (0, schema_functions_1.formatHS)(item.type, item.fieldType);
        if (hSPropTypeFieldType) {
            props.push({
                name: item.name,
                type: item.type,
                fieldType: item.fieldType,
                typeFieldType: hSPropTypeFieldType
            });
        }
    });
    return props;
}
async function createProperties(client, schemaDiff, propertyGroup) {
    if (!propertyGroup) {
        throw new actions_core_1.PayloadValidationError('"Property Group" is a required field when creating properties on an Object Schema in Hubspot');
    }
    if (schemaDiff?.missingProperties?.length === 0 && schemaDiff?.missingSensitiveProperties?.length === 0) {
        return;
    }
    const { missingProperties: props, missingSensitiveProperties: sensitiveProps } = schemaDiff;
    const input = (prop, sensitive) => {
        switch (prop.typeFieldType) {
            case types_1.HSPropTypeFieldType.NumberNumber:
                return {
                    name: prop.name,
                    label: prop.name,
                    groupName: propertyGroup,
                    type: 'number',
                    dataSensitivity: sensitive === true ? 'sensitive' : undefined,
                    fieldType: 'number'
                };
            case types_1.HSPropTypeFieldType.StringText:
            case types_1.HSPropTypeFieldType.StringTextArea:
                return {
                    name: prop.name,
                    label: prop.name,
                    groupName: propertyGroup,
                    type: 'string',
                    dataSensitivity: sensitive === true ? 'sensitive' : undefined,
                    fieldType: 'text'
                };
            case types_1.HSPropTypeFieldType.EnumerationBooleanCheckbox:
                return {
                    name: prop.name,
                    label: prop.name,
                    groupName: propertyGroup,
                    type: 'enumeration',
                    dataSensitivity: sensitive === true ? 'sensitive' : undefined,
                    fieldType: 'booleancheckbox',
                    options: [
                        {
                            label: 'true',
                            value: 'true',
                            hidden: false,
                            description: 'True',
                            displayOrder: 1
                        },
                        {
                            label: 'false',
                            value: 'false',
                            hidden: false,
                            description: 'False',
                            displayOrder: 2
                        }
                    ]
                };
            case types_1.HSPropTypeFieldType.DateTimeDate:
                return {
                    name: prop.name,
                    label: prop.name,
                    groupName: propertyGroup,
                    type: 'datetime',
                    dataSensitivity: sensitive === true ? 'sensitive' : undefined,
                    fieldType: 'date'
                };
            case types_1.HSPropTypeFieldType.DateDate:
                return {
                    name: prop.name,
                    label: prop.name,
                    groupName: propertyGroup,
                    type: 'date',
                    dataSensitivity: sensitive === true ? 'sensitive' : undefined,
                    fieldType: 'date'
                };
        }
    };
    const json = {
        inputs: [
            ...(props ? props.map((p) => input(p, false)) : []),
            ...(sensitiveProps ? sensitiveProps.map((p) => input(p, true)) : [])
        ]
    };
    await client.createPropertiesDefinition(json);
}
//# sourceMappingURL=hubspot-properties-functions.js.map