import { AlLocation, AlDefaultClient, AlGlobalizer } from '@al/core';

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */

function __awaiter(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());
    });
}

class AlWSMClientInstance {
    constructor() {
        this.serviceStack = AlLocation.YARDAPI;
    }
    /**
     * Get all appliances using an specific accountId
     * GET
     * /api/appliance/select?features=wsm_enabled&customer_ids={accountId}
     * @param accountId AIMS Account ID
     * @returns a promise with an array of appliances of a specific accountId
     *
     * @remarks
     */
    getAppliances(accountId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                version: '',
                path: `/api/appliance/select`,
                params: {
                    features: 'wsm_enabled',
                    customer_ids: accountId
                }
            });
        });
    }
    /**
     * Get an appliance's full data using an specific applianceUuid
     * GET
     * /api/wsm/config/appliance/{applianceUuid}
     *
     * @param accountId AIMS Account ID
     * @param applianceUuid Appliance UUID
     * @returns a promise with the an Appliance object with all its properties
     *
     * @remarks
     */
    getApplianceInfo(applianceUuid) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                version: '',
                path: `/api/wsm/config/appliance/${applianceUuid}`
            });
        });
    }
    /**
     * Get availability of the appliance UI
     * GET
     * /wsm/manage/appliance/${applianceUuid}/auth.html?mode=login
     *
     * @param applianceUuid Appliance UUID
     * @returns a promise with returning true if the UI is up, otherwise false
     *
     * @remarks
     */
    getWafUiAvailability(applianceUuid) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                version: '',
                path: `/api/wsm/manage/appliance/${applianceUuid}/auth.html?mode=login`
            });
        });
    }
    /**
     * Get Stats
     * GET
     * /wsm/stats/${type}/select
     *
     * @param type 'network'|'proxy'|'system'
     * @param accountId AIMS Account ID
     * @param applianceUuids Appliance UUID array of string
     * @param fields all allowed fields to get
     * @returns a promise with returning an array of StatsInfo object
     *
     * @remarks
     */
    getStats(type, accountId, applianceUuids, fields) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                version: '',
                path: `/api/wsm/stats/${type}/select`,
                params: {
                    appliance_uuids: applianceUuids.join(','),
                    customer_ids: accountId,
                    fields: fields.join(',')
                }
            });
        });
    }
}

/* tslint:disable:variable-name */
const AlWSMClient = AlGlobalizer.instantiate("AlWSMClient", () => new AlWSMClientInstance());

class ConvergenceUtilityClientInstance {
    constructor() {
        this.serviceName = 'convergence';
        this.serviceVersion = 'v2';
        this.serviceStack = AlLocation.LegacyUI;
    }
    listApplianceZones(accountId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/zonemembership`
            });
        });
    }
    getOneApplianceZone(// TODO: improve this response object
    accountId, applianceId) {
        return __awaiter(this, void 0, void 0, function* () {
            const raw = yield AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/zonemembership/${applianceId}`
            });
            return Array.isArray(raw === null || raw === void 0 ? void 0 : raw.zoneMembership) ? raw.zoneMembership[0] : raw === null || raw === void 0 ? void 0 : raw.zoneMembership;
        });
    }
    listLogSources(accountId, deploymentId, params, action) {
        return __awaiter(this, void 0, void 0, function* () {
            if (['datacenter', 'manual'].includes(deploymentId)) {
                deploymentId = 'dc-logsources';
            }
            let urlSuffix = 'collection';
            if (action) {
                urlSuffix += `_${action}`;
            }
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${urlSuffix}`
            });
        });
    }
    listProtectedHosts(accountId, deploymentId, params, action) {
        return __awaiter(this, void 0, void 0, function* () {
            let urlSuffix = 'protectedhosts';
            if (action) {
                urlSuffix += "_" + action;
            }
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${urlSuffix}`
            });
        });
    }
    listNetworks(accountId, deploymentId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/networks`
            });
        });
    }
    createNetwork(accountId, deploymentId, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.post({
                data: { source: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/networks`
            });
        });
    }
    getOneNetwork(accountId, deploymentId, networkId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/networks/${networkId}`
            });
        });
    }
    listAppliances(accountId, deploymentId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/appliances`
            });
        });
    }
    getOneAppliance(accountId, deploymentId, applianceId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/appliances/${applianceId}`
            });
        });
    }
    updateAppliance(accountId, deploymentId, applianceId, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.put({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/appliances/${applianceId}`
            });
        });
    }
    getHostHistory(accountId, hostId, type) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/history/${type}/${hostId}`
            });
        });
    }
    getAgentVersion(accountId, hostId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/utilities/agentversion/${hostId}`
            });
        });
    }
    listHosts(accountId, deploymentId, productType, params, action) {
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "lm") {
                productType = "lmhosts";
            }
            else if (productType === "tm") {
                productType = "tmhosts";
            }
            if (action) {
                productType += "_" + action;
            }
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${productType}`
            });
        });
    }
    listCredentials(accountId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/credentials`
            });
        });
    }
    getOneCredential(accountId, credentialId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/credentials/${credentialId}`
            });
        });
    }
    deleteOneCredential(accountId, credentialId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.delete({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/credentials/${credentialId}`
            });
        });
    }
    listAlertRules(accountId, productType, params) {
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "log-collection") {
                productType = "lm_collection";
            }
            else if (productType === "intrusion-detection") {
                productType = "tm_collection";
            }
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/alertrules/${productType}`
            });
        });
    }
    getAlertRule(accountId, productType, ruleId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "log-collection") {
                productType = "lm_collection";
            }
            else if (productType === "intrusion-detection") {
                productType = "tm_collection";
            }
            const response = yield AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/alertrules/${productType}/${ruleId}`
            });
            return response.policy;
        });
    }
    deleteAlertRule(accountId, productType, ruleId) {
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "log-collection") {
                productType = "lm_collection";
            }
            else if (productType === "intrusion-detection") {
                productType = "tm_collection";
            }
            return AlDefaultClient.delete({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/alertrules/${productType}/${ruleId}`
            });
        });
    }
    updateAlertRule(accountId, productType, ruleId, data) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "log-collection") {
                productType = "lm_collection";
            }
            else if (productType === "intrusion-detection") {
                productType = "tm_collection";
            }
            const response = yield AlDefaultClient.put({
                data: { policy: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/alertrules/${productType}/${ruleId}`
            });
            if (((_a = response === null || response === void 0 ? void 0 : response.policies) !== null && _a !== void 0 ? _a : []).length === 1) {
                const { policy } = response.policies[0];
                return policy;
            }
            return null;
        });
    }
    createAlertRule(accountId, productType, data) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            if (productType === "log-collection") {
                productType = "lm_collection";
            }
            else if (productType === "intrusion-detection") {
                productType = "tm_collection";
            }
            const response = yield AlDefaultClient.post({
                data: { policy: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/alertrules/${productType}`
            });
            return (_a = response === null || response === void 0 ? void 0 : response.policy) !== null && _a !== void 0 ? _a : null;
        });
    }
    createPolicy(accountId, policyType, data) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const raw = yield AlDefaultClient.post({
                data: { policy: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}`
            });
            return (_a = raw === null || raw === void 0 ? void 0 : raw.policy) !== null && _a !== void 0 ? _a : raw;
        });
    }
    getOnePolicy(accountId, policyType, policyId) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const raw = yield AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}/${policyId}`
            });
            return (_a = raw === null || raw === void 0 ? void 0 : raw.policy) !== null && _a !== void 0 ? _a : raw;
        });
    }
    isPolicyDeletable(accountId, policyType, policyId) {
        return __awaiter(this, void 0, void 0, function* () {
            const { isDeleteable } = yield AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}/${policyId}/deleteable`
            });
            return isDeleteable;
        });
    }
    isCredentialDeletable(accountId, credentialId) {
        return __awaiter(this, void 0, void 0, function* () {
            const { isDeleteable } = yield AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/credentials/${credentialId}/deleteable`
            });
            return isDeleteable;
        });
    }
    deletePolicy(accountId, policyType, policyId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.delete({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}/${policyId}`
            });
        });
    }
    listPolicies(accountId, policyType, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}`
            });
        });
    }
    updatePolicy(accountId, policyType, policyId, data) {
        var _a;
        return __awaiter(this, void 0, void 0, function* () {
            const raw = yield AlDefaultClient.put({
                data: { policy: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/policies/${policyType}/${policyId}`
            });
            return (_a = raw === null || raw === void 0 ? void 0 : raw.policy) !== null && _a !== void 0 ? _a : raw;
        });
    }
    updateCollector(accountId, deploymentId, collectorId, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.put({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/collectors/${collectorId}`
            });
        });
    }
    getOneCollector(accountId, deploymentId, collectorId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/collectors/${collectorId}`
            });
        });
    }
    listCollectors(accountId, deploymentId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/collectors`
            });
        });
    }
    createCollection(accountId, deploymentId, data, entityType = 'collection') {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.post({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${entityType}`
            });
        });
    }
    updateCollection(accountId, deploymentId, collectionId, data, entityType = 'collection') {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.put({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${entityType}/${collectionId}`
            });
        });
    }
    getCloudExplorerAwsRegions(accountId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/cloud-explorer/regions`
            });
        });
    }
    deleteCollectionSource(accountId, deploymentId, collectionId, entityType = 'collection', action) {
        return __awaiter(this, void 0, void 0, function* () {
            if (action) {
                entityType += `_${action}`;
            }
            return AlDefaultClient.delete({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/${entityType}/${collectionId}`
            });
        });
    }
    getCollectionSource(accountId, deploymentId, collectionId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/collection/${collectionId}`
            });
        });
    }
    listCollectionSources(accountId, deploymentId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/${deploymentId}/collection`
            });
        });
    }
    listCloudDefenderCollectionSources(accountId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/deployments/dc-logsources/collection`
            });
        });
    }
    addToCase(accountId, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.post({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/utility/case/add`
            });
        });
    }
    getFiltersByEntityType(accountId, entityType) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/utilities/filters/${entityType}`,
            });
        });
    }
    lookUpUsers(accountId, userIds) {
        return __awaiter(this, void 0, void 0, function* () {
            const users = userIds.join(',');
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/utility/userlookup`,
                params: { users }
            });
        });
    }
    getDownloads(accountId, product) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/utilities/downloads/${product}`,
            });
        });
    }
    getApiKey(accountId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/apikey`,
            });
        });
    }
    listCertificateKeyPairs(accountId, params) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.get({
                params,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/certificates`
            });
        });
    }
    createCertificateKeyPair(accountId, data) {
        return __awaiter(this, void 0, void 0, function* () {
            const { keypair } = yield AlDefaultClient.post({
                data: { keypair: data },
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/certificates`
            });
            return keypair;
        });
    }
    deleteCertificateKeyPair(accountId, keypairId) {
        return __awaiter(this, void 0, void 0, function* () {
            return AlDefaultClient.delete({
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/certificates/${keypairId}`
            });
        });
    }
    validatePemFile(accountId, file) {
        return __awaiter(this, void 0, void 0, function* () {
            const data = new FormData();
            data.append('file', file);
            return AlDefaultClient.post({
                data,
                service_stack: this.serviceStack,
                service_name: this.serviceName,
                version: this.serviceVersion,
                account_id: accountId,
                path: `/certificates/pemvalidation`
            });
        });
    }
    exportRecords(accountId, deploymentId, type, params) {
        return AlDefaultClient.get({
            params,
            service_stack: this.serviceStack,
            service_name: this.serviceName,
            version: this.serviceVersion,
            account_id: accountId,
            path: `/deployments/${deploymentId}/${type}_export`
        });
    }
}

/**
 * Nothing is complete without at least two aliases
 */
/* tslint:disable:variable-name */
const AlConvergenceUtilityClient = AlGlobalizer.instantiate('al.convergence-utility', () => new ConvergenceUtilityClientInstance());

export { AlConvergenceUtilityClient, AlWSMClient, AlWSMClientInstance, ConvergenceUtilityClientInstance };
//# sourceMappingURL=al-core-defender.esm2015.js.map
