// Copyright (c) 2009-2026 SAP SE, All Rights Reserved

/**
 * @fileOverview Module which stores the current state of the cold start.
 */

sap.ui.define([
], (
) => {
    "use strict";

    /**
     * @enum {string} sap.ushell.navigation.ColdStart.State
     *
     * @since 1.144.0
     * @private
     */
    const State = {
        /**
         * Initial State of the FLP
         */
        Cold: "Cold",
        /**
         * A navigation happened, but it might be reverted
         * Any navigation in this states triggers the running state
         */
        Pending: "Pending",
        /**
         * The FLP is running and not in cold start anymore
         */
        Running: "Running"
    };

    /**
     * @alias sap.ushell.state.ColdStart
     * @namespace
     * @description Module that stores the cold start state.
     * This state is used to modify the navigation mode during the initial FLP load.
     * With this navigations which are regularly explace are run inplace during cold start.
     * (newWindowThenEmbedded)
     *
     * @since 1.144.0
     * @private
     */
    class ColdStart {
        #sCurrentState = State.Cold;

        /**
         * Tracks navigation during cold start.
         * @param {boolean} [bCanBeUntracked=true] whether the navigation can be reverted again
         *
         * @since 1.144.0
         * @private
         */
        trackNavigation (bCanBeUntracked = true) {
            if (this.#sCurrentState === State.Running) {
                // once running it can never get cold again
                return;
            }

            // only go to running con consecutive navigation
            if (this.#sCurrentState === State.Pending || !bCanBeUntracked) {
                this.#sCurrentState = State.Running;
                return;
            }

            this.#sCurrentState = State.Pending;
        }

        /**
         * Untracks navigation during cold start.
         * E.g. during trampolin or explace navigation.
         *
         * @since 1.144.0
         * @private
         */
        untrackNavigation () {
            if (this.#sCurrentState === State.Running) {
                // once running it can never get cold again
                return;
            }

            this.#sCurrentState = State.Cold;
        }

        /**
         * Checks whether an application is cold started.
         * This method is scoped to checking the cold start conditions of applications only.
         *
         * - page is refreshed
         * - Tab was opened explace by another tab
         * - URL is pasted in a new tab
         *
         * @returns {boolean} whether the application is in a cold start state
         *
         * @since 1.144.0
         * @private
         */
        isColdStart () {
            return this.#sCurrentState !== State.Running;
        }

        /**
         * ONLY FOR TESTING!
         * Resets the current cold start state.
         *
         * @since 1.144.0
         * @private
         */
        reset () {
            this.#sCurrentState = State.Cold;
        }
    }

    return new ColdStart();
});
