"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TactfulClient = exports.UCXClient = void 0;
const auth_helper_1 = require("./auth-helper");
const channels_1 = require("@tactfulai/channels");
const livechat_client_1 = require("@tactfulai/livechat-client");
const tenant_sdk_1 = require("@tactfulai/tenant-sdk");
const ai_sdk_1 = require("@tactfulai/ai-sdk");
const erp_sdk_1 = require("@tactfulai/erp-sdk");
const erp_sdk_v2_1 = require("@tactfulai/erp-sdk-v2");
const attachments_sdk_1 = require("@tactfulai/attachments-sdk");
const auth_1 = require("@tactfulai/auth");
const automation_client_1 = require("@tactfulai/automation-client");
class UCXClient {
    constructor(domainFormat = 'https://{svc}.tactful.ai', apiSubdomain = 'api', accessToken, isInternal) {
        this.domainFormat = 'https://{svc}.tactful.ai';
        this.apiSubdomain = 'api';
        this.domainFormat = domainFormat;
        this.apiSubdomain = apiSubdomain;
        this.accessToken = accessToken;
        this.isInternal = isInternal;
        this.initialize();
    }
    getSDKsUrls() {
        if (this.isInternal) {
            return {
                auth: process.env.AUTH_HOST_URL,
                tenants: process.env.TENANTS_HOST_URL,
                erp: process.env.ERP_HOST_URL,
                erp_v2: process.env.ERP_V2_HOST_URL,
                attachments: process.env.ATTACHMENTS_HOST_URL,
                livechat: process.env.LIVECHAT_HOST_URL,
                livechatReader: process.env.LIVECHAT_READER_HOST_URL,
                channels: {
                    email: process.env.EMAIL_HOST_URL,
                    meta: process.env.CHANNELS_HOST_URL,
                    webchat: process.env.WEBCHAT_HOST_URL
                },
                ai: {
                    ai: process.env.AI_API_HOST_URL + '/nlu',
                    nlu: process.env.NLU_API_HOST_URL + '/nlu',
                    statistics: process.env.MODEL_STATISTICS_HOST_URL + '/nlu',
                    analysis: process.env.ANALYSIS_HOST_URL,
                    openAI: process.env.OPENAI_API_HOST_URL + '/openai'
                },
                automation: process.env.AUTOMATION_API_HOST_URL
            };
        }
        const apiUrl = this.domainFormat.replace('{svc}', this.apiSubdomain);
        return {
            auth: `${apiUrl}/auth/v1`,
            tenants: `${apiUrl}/tenants/v1`,
            erp: `${apiUrl}/erp/v1`,
            erp_v2: `${apiUrl}/erp/v2`,
            attachments: `${apiUrl}/attachments/v1`,
            livechat: this.domainFormat.replace('{svc}', 'livechat-server'),
            livechatReader: this.domainFormat.replace('{svc}', 'livechat-reader'),
            channels: {
                email: this.domainFormat.replace('{svc}', 'email'),
                meta: this.domainFormat.replace('{svc}', 'channels'),
                webchat: this.domainFormat.replace('{svc}', 'webchat')
            },
            ai: {
                ai: this.domainFormat.replace('{svc}', 'ai-api') + '/nlu',
                nlu: this.domainFormat.replace('{svc}', 'nlu-api') + '/nlu',
                statistics: this.domainFormat.replace('{svc}', 'model-statistics') + '/nlu',
                analysis: this.domainFormat.replace('{svc}', 'analysis'),
                openAI: this.domainFormat.replace('{svc}', 'openai-api') + '/openai',
            },
            automation: `${apiUrl}/automation-api`
        };
    }
    // Initialize the core object with all namespace configs
    initialize() {
        const sdkUrls = this.getSDKsUrls();
        const accessToken = this.accessToken;
        this.auth = (0, auth_1.initializeIsolatedTactful)({ basePath: sdkUrls.auth, apiKey: accessToken });
        this.tenants = (0, tenant_sdk_1.initializeIsolatedTactful)({ basePath: sdkUrls.tenants, apiKey: accessToken });
        this.erp = (0, erp_sdk_1.initializeIsolatedTactful)({ basePath: sdkUrls.erp, apiKey: accessToken });
        this.erp_v2 = (0, erp_sdk_v2_1.initializeIsolatedTactful)({ basePath: sdkUrls.erp_v2, apiKey: accessToken });
        this.attachments = (0, attachments_sdk_1.initializeIsolatedTactful)({ basePath: sdkUrls.attachments, apiKey: accessToken });
        this.livechat = (0, livechat_client_1.initializeIsolatedLivechatServer)({ basePath: sdkUrls.livechat, apiKey: accessToken });
        this.liveCommands = (0, livechat_client_1.initializeIsolatedLiveCommands)({ basePath: sdkUrls.livechat, apiKey: accessToken });
        this.livechatReader = (0, livechat_client_1.initializeIsolatedLivechatServer)({ basePath: sdkUrls.livechatReader, apiKey: accessToken });
        this.liveCommandsReader = (0, livechat_client_1.initializeIsolatedLiveCommands)({ basePath: sdkUrls.livechatReader, apiKey: accessToken });
        this.channels = new channels_1.ChannelSDK({
            accessToken,
            basePathEmail: sdkUrls.channels.email,
            basePathMeta: sdkUrls.channels.meta,
            basePathWebChat: sdkUrls.channels.webchat,
            apiKey: accessToken
        });
        this.ai = new ai_sdk_1.IsolatedAISDK({
            apiKey: accessToken,
            basePathAI: sdkUrls.ai.ai,
            basePathNlu: sdkUrls.ai.nlu,
            basePatStatistics: sdkUrls.ai.statistics,
            basePathAnalysis: sdkUrls.ai.analysis,
            basePathOpenAI: sdkUrls.ai.openAI
        });
        this.automation = (0, automation_client_1.initializeIsolatedTactful)({
            basePath: sdkUrls.automation,
            apiKey: accessToken,
            accessToken
        });
    }
    loginWithCredentials(username, password, profile_id) {
        return __awaiter(this, void 0, void 0, function* () {
            let credentials = yield this.auth.oauth.postOauthLogin({ payload: { username, password, profile_id } });
            this.accessToken = "Bearer " + credentials.access_token;
            this.accessTokenExpiresIn = credentials.expires_in;
            this.initialize();
            return credentials;
        });
    }
    loginWithToken(token, expires_in) {
        this.accessToken = "Bearer " + token;
        this.accessTokenExpiresIn = expires_in;
        this.initialize();
    }
    loginWithOAuth(OauthCreds, profile_id) {
        return __awaiter(this, void 0, void 0, function* () {
            const serviceToken = new auth_helper_1.ServiceToken(OauthCreds);
            const token = yield serviceToken.getToken(profile_id);
            this.accessToken = "Bearer " + token.access_token;
            this.accessTokenExpiresIn = token.expires_in;
            this.initialize();
            return token;
        });
    }
    checkTokenExpiration() {
        const currentTimeInSeconds = Math.floor(Date.now() / 1000);
        return this.accessTokenExpiresIn > currentTimeInSeconds;
    }
}
exports.UCXClient = UCXClient;
exports.TactfulClient = UCXClient;
//# sourceMappingURL=index.js.map