"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getContactLists = exports.getFields = exports.getEvents = exports.createWsseHeader = exports.API_BASE = exports.API_PATH = exports.API_HOST = void 0;
const crypto_1 = require("crypto");
exports.API_HOST = 'https://api.emarsys.net';
exports.API_PATH = '/api/v2/';
exports.API_BASE = `${exports.API_HOST}${exports.API_PATH}`;
const createWsseHeader = (settings) => {
    const nonce = (0, crypto_1.randomBytes)(16).toString('hex');
    const hash = (0, crypto_1.createHash)('sha1');
    const ts = new Date().toISOString();
    const secret = nonce + ts + settings.api_password;
    const secret_hash = Buffer.from(hash.update(secret).digest('hex')).toString('base64');
    const auth_header = `UsernameToken Username="${settings.api_user}", PasswordDigest="${secret_hash}", Nonce="${nonce}", Created="${ts}"`;
    return auth_header;
};
exports.createWsseHeader = createWsseHeader;
const getEvents = async (request) => {
    const data = await request(`${exports.API_BASE}event`);
    const choices = [];
    if (data && data.content) {
        const api_data = JSON.parse(data.content);
        if (api_data && api_data.replyCode !== undefined && api_data.replyCode == 0) {
            if (api_data.data && Array.isArray(api_data.data) && api_data.data.length > 0) {
                for (let a = 0; a < api_data.data.length; a++) {
                    choices.push({
                        label: api_data.data[a].name,
                        value: api_data.data[a].id
                    });
                }
            }
        }
    }
    const events = {
        choices: choices
    };
    return events;
};
exports.getEvents = getEvents;
const getFields = async (request) => {
    const data = await request(`${exports.API_BASE}field/translate/en`);
    const choices = [];
    if (data && data.content) {
        const api_data = JSON.parse(data.content);
        if (api_data && api_data.replyCode !== undefined && api_data.replyCode == 0) {
            if (api_data.data && Array.isArray(api_data.data) && api_data.data.length > 0) {
                for (let a = 0; a < api_data.data.length; a++) {
                    choices.push({
                        label: api_data.data[a].name,
                        value: api_data.data[a].id
                    });
                }
            }
        }
    }
    const fields = {
        choices: choices
    };
    return fields;
};
exports.getFields = getFields;
const getContactLists = async (request) => {
    const data = await request(`${exports.API_BASE}contactlist`);
    const choices = [];
    if (data && data.content) {
        const api_data = JSON.parse(data.content);
        if (api_data && api_data.replyCode !== undefined && api_data.replyCode == 0) {
            if (api_data.data && Array.isArray(api_data.data) && api_data.data.length > 0) {
                for (let a = 0; a < api_data.data.length; a++) {
                    choices.push({
                        label: api_data.data[a].name,
                        value: api_data.data[a].id
                    });
                }
            }
        }
    }
    const lists = {
        choices: choices
    };
    return lists;
};
exports.getContactLists = getContactLists;
//# sourceMappingURL=emarsys-helper.js.map