"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.channelToDataStore = exports.isDataStoreAliasMessage = void 0;
const container_definitions_1 = require("@fluidframework/container-definitions");
const internal_1 = require("@fluidframework/core-utils/internal");
const internal_2 = require("@fluidframework/telemetry-utils/internal");
const messageTypes_js_1 = require("./messageTypes.js");
/**
 * Type guard that returns true if the given alias message is actually an instance of
 * a class which implements {@link IDataStoreAliasMessage}
 * @param maybeDataStoreAliasMessage - message object to be validated
 * @returns True if the {@link IDataStoreAliasMessage} is fully implemented, false otherwise
 */
const isDataStoreAliasMessage = (maybeDataStoreAliasMessage) => {
    return (typeof maybeDataStoreAliasMessage?.internalId ===
        "string" &&
        typeof maybeDataStoreAliasMessage?.alias === "string");
};
exports.isDataStoreAliasMessage = isDataStoreAliasMessage;
const channelToDataStore = (fluidDataStoreChannel, internalId, channelCollection, logger) => new DataStore(fluidDataStoreChannel, internalId, channelCollection, logger);
exports.channelToDataStore = channelToDataStore;
var AliasState;
(function (AliasState) {
    AliasState["Aliased"] = "Aliased";
    AliasState["Aliasing"] = "Aliasing";
    AliasState["None"] = "None";
})(AliasState || (AliasState = {}));
class DataStore {
    /**
     * {@inheritDoc @fluidframework/runtime-definitions#IDataStore.trySetAlias}
     */
    async trySetAlias(alias) {
        if (alias.includes("/")) {
            throw new internal_2.UsageError(`The alias cannot contain slashes: '${alias}'`);
        }
        switch (this.aliasState) {
            // If we're already aliasing, check if it's for the same value and return
            // the stored promise, otherwise return 'AlreadyAliased'
            case AliasState.Aliasing: {
                (0, internal_1.assert)(this.aliasResult !== undefined, 0x316 /* There should be a cached promise of in-progress aliasing */);
                await this.aliasResult;
                return this.alias === alias ? "Success" : "AlreadyAliased";
            }
            // If this datastore is already aliased, return true only if this
            // is a repeated call for the same alias
            case AliasState.Aliased: {
                return this.alias === alias ? "Success" : "AlreadyAliased";
            }
            case AliasState.None: {
                const existingAlias = this.pendingAliases.get(alias);
                if (existingAlias !== undefined) {
                    // There is already another datastore which will be aliased
                    // to the same name
                    return "Conflict";
                }
                // There is no current or past alias operation for this datastore,
                // or for this alias, so it is safe to continue execution
                break;
            }
            default: {
                (0, internal_1.unreachableCase)(this.aliasState);
            }
        }
        this.aliasState = AliasState.Aliasing;
        this.aliasResult = this.trySetAliasInternal(alias);
        this.pendingAliases.set(alias, this.aliasResult);
        return this.aliasResult;
    }
    async trySetAliasInternal(alias) {
        const message = {
            internalId: this.internalId,
            alias,
        };
        this.fluidDataStoreChannel.makeVisibleAndAttachGraph();
        if (this.parentContext.attachState === container_definitions_1.AttachState.Detached) {
            const localResult = this.channelCollection.processAliasMessageCore(this.internalId, alias);
            // Explicitly lock-out future attempts of aliasing,
            // regardless of result
            this.aliasState = AliasState.Aliased;
            return localResult ? "Success" : "Conflict";
        }
        const aliased = await this.ackBasedPromise((resolve) => {
            this.parentContext.submitMessage(messageTypes_js_1.ContainerMessageType.Alias, message, resolve);
        })
            .catch((error) => {
            this.logger.sendErrorEvent({
                eventName: "AliasingException",
                alias: {
                    value: alias,
                    tag: internal_2.TelemetryDataTag.UserData,
                },
                internalId: {
                    value: this.internalId,
                    tag: internal_2.TelemetryDataTag.CodeArtifact,
                },
            }, error);
            return false;
        })
            .finally(() => {
            this.pendingAliases.delete(alias);
        });
        if (!aliased) {
            this.aliasState = AliasState.None;
            this.aliasResult = undefined;
            return "Conflict";
        }
        this.alias = alias;
        this.aliasState = AliasState.Aliased;
        return "Success";
    }
    /**
     * {@inheritDoc @fluidframework/runtime-definitions#IDataStore.entryPoint}
     */
    get entryPoint() {
        return this.fluidDataStoreChannel.entryPoint;
    }
    constructor(fluidDataStoreChannel, internalId, channelCollection, logger, parentContext = channelCollection.parentContext) {
        this.fluidDataStoreChannel = fluidDataStoreChannel;
        this.internalId = internalId;
        this.channelCollection = channelCollection;
        this.logger = logger;
        this.parentContext = parentContext;
        this.aliasState = AliasState.None;
        this.pendingAliases = channelCollection.pendingAliases;
    }
    async ackBasedPromise(executor) {
        let rejectBecauseDispose;
        return new Promise((resolve, reject) => {
            rejectBecauseDispose = () => reject(new Error("ContainerRuntime disposed while this ack-based Promise was pending"));
            if (this.parentContext.containerRuntime.disposed) {
                rejectBecauseDispose();
                return;
            }
            this.parentContext.containerRuntime.on("dispose", rejectBecauseDispose);
            executor(resolve, reject);
        }).finally(() => {
            this.parentContext.containerRuntime.off("dispose", rejectBecauseDispose);
        });
    }
}
//# sourceMappingURL=dataStore.js.map