"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.gen_customer_taxonomy_payload = gen_customer_taxonomy_payload;
exports.gen_segment_subtaxonomy_payload = gen_segment_subtaxonomy_payload;
exports.gen_random_id = gen_random_id;
exports.gen_oauth1_signature = gen_oauth1_signature;
exports.update_taxonomy = update_taxonomy;
const crypto_1 = require("crypto");
const actions_core_1 = require("@segment/actions-core");
function gen_customer_taxonomy_payload(settings) {
    const data = {
        id: settings.engage_space_id,
        name: settings.engage_space_id,
        description: settings.customer_desc,
        users: {
            include: [settings.mdm_id]
        }
    };
    const req_body_form = `--SEGMENT-DATA\r\nContent-Disposition: form-data; name="metadata"\r\nContent-Type: application/json;charset=UTF-8\r\n\r\n{ "description" : "${settings.customer_desc}" }\r\n--SEGMENT-DATA\r\nContent-Disposition: form-data; name="data"\r\nContent-Type: application/json;charset=UTF-8\r\n\r\n${JSON.stringify(data)}\r\n--SEGMENT-DATA--`;
    return req_body_form;
}
function gen_segment_subtaxonomy_payload(payload) {
    const data = {
        id: payload.segment_audience_id,
        name: payload.segment_audience_key,
        type: 'SEGMENT'
    };
    const req_body_form = `--SEGMENT-DATA\r\nContent-Disposition: form-data; name="metadata"\r\nContent-Type: application/json;charset=UTF-8\r\n\r\n{ "description" : "Create segment ${data.id}" }\r\n--SEGMENT-DATA\r\nContent-Disposition: form-data; name="data"\r\nContent-Type: application/json;charset=UTF-8\r\n\r\n${JSON.stringify(data)}\r\n--SEGMENT-DATA--`;
    return req_body_form;
}
function gen_random_id(length) {
    const pattern = 'abcdefghijklmnopqrstuvwxyz123456789';
    const random_id = [];
    for (let i = 0; i < length; i++) {
        random_id.push(pattern[Math.floor(Math.random() * pattern.length)]);
    }
    return random_id.join('');
}
function gen_oauth1_signature(client_key, client_secret, method, url) {
    const timestamp = Math.floor(new Date().getTime() / 1000);
    const nonce = gen_random_id(15);
    const param_string = `oauth_consumer_key=${encodeURIComponent(client_key)}&oauth_nonce=${encodeURIComponent(nonce)}&oauth_signature_method=${encodeURIComponent('HMAC-SHA1')}&oauth_timestamp=${encodeURIComponent(timestamp)}&oauth_version=${encodeURIComponent('1.0')}`;
    const base_string = `${method.toUpperCase()}&${encodeURIComponent(url)}&${encodeURIComponent(param_string)}`;
    const encoded_client_secret = encodeURIComponent(client_secret);
    const signature = encodeURIComponent((0, crypto_1.createHmac)('sha1', encoded_client_secret + '&')
        .update(base_string)
        .digest('base64'));
    const oauth1_auth_string = `OAuth oauth_consumer_key="${client_key}", oauth_nonce="${nonce}", oauth_signature="${signature}", oauth_signature_method="HMAC-SHA1", oauth_timestamp="${timestamp}", oauth_version="1.0"`;
    return oauth1_auth_string;
}
async function update_taxonomy(engage_space_id, tx_creds, request, body_form_data, statsClient, statsTags) {
    const tx_client_secret = tx_creds.tx_client_secret;
    const tx_client_key = tx_creds.tx_client_key;
    const url = `https://datax.yahooapis.com/v1/taxonomy/append${engage_space_id.length > 0 ? '/' + engage_space_id : ''}`;
    const oauth1_auth_string = gen_oauth1_signature(tx_client_key, tx_client_secret, 'PUT', url);
    try {
        const add_segment_node = await request(url, {
            method: 'PUT',
            body: body_form_data,
            headers: {
                Authorization: oauth1_auth_string,
                'Content-Type': 'multipart/form-data; boundary=SEGMENT-DATA'
            }
        });
        if (statsClient && statsTags) {
            statsClient.incr('update_taxonomy.success', 1, statsTags);
        }
        return await add_segment_node.json();
    }
    catch (error) {
        const _error = error;
        if (statsClient && statsTags) {
            statsClient.incr('update_taxonomy.error', 1, statsTags);
        }
        if (parseInt(_error.response.status) == 401) {
            throw new actions_core_1.IntegrationError(`Error while updating taxonomy: ${JSON.stringify(_error.response.data)} ${_error.response.status}. Validate Yahoo credentials`, 'YAHOO_TAXONOMY_API_AUTH_ERR', 400);
        }
        else {
            throw error;
        }
    }
}
//# sourceMappingURL=utils-tax.js.map