/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { createStore } from 'zustand/vanilla';
import { useStore } from 'zustand';
import { configLogger } from '../../utils/logger.js';
let loadConfigurationFromServer = true;
let initialConfiguration = {
    runUrl: 'https://prod1.datalayer.run',
    token: '',
    credits: 0,
    cpuEnvironment: 'ai-agents-env"',
    gpuEnvironment: 'ai-env',
    useMock: false,
    whiteLabel: true,
    loadConfigurationFromServer: true,
    jupyterServerless: false,
    iamRunUrl: 'https://prod1.datalayer.run',
    runtimesRunUrl: 'https://r1.datalayer.run',
    libraryRunUrl: 'https://prod1.datalayer.run',
    spacerRunUrl: 'https://prod1.datalayer.run',
    aiagentsRunUrl: 'https://prod1.datalayer.run',
    aiinferenceRunUrl: 'https://prod1.datalayer.run',
    mcpserversRunUrl: 'https://prod1.datalayer.run',
    otelRunUrl: 'https://prod1.datalayer.run',
    growthRunUrl: 'https://prod1.datalayer.run',
    inboundsRunUrl: 'https://prod1.datalayer.run',
    successRunUrl: 'https://prod1.datalayer.run',
    supportRunUrl: 'https://prod1.datalayer.run',
    launcher: {
        category: 'Datalayer',
        name: 'Datalayer Runtimes',
        icon: 'https://raw.githubusercontent.com/datalayer/icons/main/svg/data1/jupyter-base.svg',
        rank: 1,
    },
    brand: {
        name: 'Datalayer',
        about: 'AI Agents for Data Analysis',
        logoUrl: 'https://assets.datalayer.tech/datalayer-25.svg',
        logoSquareUrl: 'https://assets.datalayer.tech/datalayer-square.png',
        copyright: '© 2025 Datalayer, Inc',
        docsUrl: 'https://docs.datalayer.ai',
        supportUrl: 'https://datalayer.ai/support',
        termsUrl: 'https://datalayer.ai/terms',
        pricingUrl: 'https://datalayer.ai/pricing',
        privacyUrl: 'https://datalayer.ai/privacy',
    },
};
// Try loading initial state from datalayer-config-data element
try {
    if (typeof document !== 'undefined') {
        const rawConfig = document.getElementById('datalayer-config-data');
        if (rawConfig?.innerText) {
            const htmlOverridingConfiguration = JSON.parse(rawConfig?.innerText || '{}');
            if (htmlOverridingConfiguration.loadConfigurationFromServer != undefined) {
                loadConfigurationFromServer =
                    htmlOverridingConfiguration.loadConfigurationFromServer;
            }
            initialConfiguration = {
                ...initialConfiguration,
                ...htmlOverridingConfiguration,
            };
            configLogger.info('Datalayer configuration loaded from HTML page', initialConfiguration);
            window.document.title = `${initialConfiguration.brand.name} ☰ ${initialConfiguration.brand.about}`;
        }
    }
}
catch (error) {
    configLogger.debug('No valid configuration found in the webpage.', error);
}
export const coreStore = createStore((set, get) => ({
    tab: 0.0,
    getIntTab: () => Math.floor(get().tab),
    setTab: (tab) => set((state) => ({ tab })),
    configuration: initialConfiguration,
    setConfiguration: (configuration) => {
        configLogger.debug('Setting Datalayer configuration', configuration);
        set(state => ({
            configuration: {
                ...state.configuration,
                ...configuration,
            },
        }));
    },
    version: '',
    setVersion: version => {
        if (version && !get().version) {
            set(state => ({ version }));
        }
    },
    loadConfigurationFromServer,
    setLoadConfigurationFromServer: (loadConfigurationFromServer) => {
        set(state => ({ loadConfigurationFromServer }));
    },
}));
export function useCoreStore(selector) {
    return useStore(coreStore, selector);
}
export default useCoreStore;
