"use strict";
/*
 *  Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *  SPDX-License-Identifier: Apache-2.0
 */
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = void 0;
/* eslint-disable security/detect-object-injection */
const workbench_core_environments_1 = require("@aws/workbench-core-environments");
const lodash_1 = __importDefault(require("lodash"));
/* eslint-disable-next-line */
async function handler(event) {
    var _a;
    console.log(`StatusHandler processing event.`);
    // Various environment types indicate status in unique locations of their event detail
    // This is to standardize each of them
    const statusLocation = {
        // This is the source used by SSM automation docs to launch/terminate environments
        automation: 'Status',
        sagemaker: 'NotebookInstanceStatus'
        // Add your new env types here
    };
    // Environment types could use different terminologies for their instance names (what we use for "INID#<instanceId>")
    // This is to standardize each of them
    const instanceIdLocation = {
        sagemaker: 'NotebookInstanceName'
        // Add your new env types here
    };
    // Various environment types indicate instance names and ARNs differently in ServiceCatalog record outputs
    // This is to standardize each of them
    const envTypeRecordOutputKeys = {
        // This is the `envType` defined in the "LaunchSSM.yaml" SSM document
        sagemakerNotebook: {
            instanceNameRecordKey: 'NotebookInstanceName',
            instanceArnRecordKey: 'NotebookArn',
            instanceRoleName: 'EnvironmentInstanceRoleArn'
        }
        // Add your new env types here
    };
    // Some env types use different terminologies for statuses
    const alternateStatuses = {
        sagemaker: {
            InService: 'COMPLETED',
            Deleting: 'TERMINATING',
            Deleted: 'TERMINATED'
        }
        // Add your new env alternate statuses here
    };
    const source = event.source === 'automation' ? 'automation' : event.source.split('aws.')[1];
    if (!lodash_1.default.includes(Object.keys(statusLocation), source)) {
        console.log('The source for this event is not recognized by Status Handler. Skipping operation...');
        return;
    }
    const recordOutputKeys = { instanceName: '', instanceArn: '', instanceRoleName: '' };
    let instanceId;
    let status;
    status = lodash_1.default.get(event.detail, statusLocation[source]);
    if (source === 'automation') {
        // For when events arise from launch/terminate operations (SSM docs)
        const envType = event.detail.EnvType;
        recordOutputKeys.instanceName = envTypeRecordOutputKeys[envType].instanceNameRecordKey;
        recordOutputKeys.instanceArn = envTypeRecordOutputKeys[envType].instanceArnRecordKey;
        recordOutputKeys.instanceRoleName = envTypeRecordOutputKeys[envType].instanceRoleName;
    }
    else {
        // For when events arise from start/stop operations (not from SSM docs)
        if (lodash_1.default.includes(Object.keys(alternateStatuses[source]), status))
            status = alternateStatuses[source][status];
        instanceId = lodash_1.default.get(event.detail, instanceIdLocation[source]);
    }
    // Map event to EventBridgeEventToDDB
    const ebToDDB = {
        envId: event.detail.EnvId || ((_a = event.detail.Tags) === null || _a === void 0 ? void 0 : _a.Env),
        instanceId,
        recordOutputKeys,
        status: status === null || status === void 0 ? void 0 : status.toUpperCase(),
        // TODO: This propagates error messages (if any) for launch/terminate failure events. Add logic for propagating start/stop failure event messages
        errorMsg: lodash_1.default.isObject(event.detail.ErrorMessage)
            ? JSON.stringify(event.detail.ErrorMessage)
            : event.detail.ErrorMessage,
        operation: event.detail.Operation,
        metadata: event
    };
    const statusHandler = new workbench_core_environments_1.StatusHandler();
    await statusHandler.execute(ebToDDB);
}
exports.handler = handler;
//# sourceMappingURL=statusHandlerLambda.js.map