var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { UmbTextStyles } from '../core/style/index.js';
import { css, html, customElement, state, query, when } from '../../external/lit/index.js';
import { UmbLitElement } from '../core/lit-element/index.js';
import { ProfilingService } from '../core/backend-api/index.js';
import { tryExecute } from '../core/resources/index.js';
import { consumeContext } from '../../libs/context-api/index.js';
import { UMB_NOTIFICATION_CONTEXT } from '../core/notification/index.js';
let UmbDashboardPerformanceProfilingElement = class UmbDashboardPerformanceProfilingElement extends UmbLitElement {
    constructor() {
        super(...arguments);
        this._profilingStatus = true;
        // TODO: Get this from the management api configuration when available
        this._isDebugMode = true;
        this._isLoading = true;
    }
    #setToggle(value) {
        this._toggle.checked = value;
        this._profilingStatus = value;
        this._isLoading = false;
    }
    async firstUpdated() {
        const status = await this.#getProfilingStatus();
        this.#setToggle(status);
    }
    async #getProfilingStatus() {
        const { data } = await tryExecute(this, ProfilingService.getProfilingStatus());
        return data?.enabled ?? false;
    }
    async #disableProfilingStatus() {
        this._isLoading = true;
        const { error } = await tryExecute(this, ProfilingService.putProfilingStatus({ body: { enabled: false } }));
        if (error) {
            this.#setToggle(true);
            return;
        }
        // Test that it was actually disabled
        const status = await this.#getProfilingStatus();
        if (status) {
            this.#setToggle(true);
            this._notificationContext?.peek('warning', {
                data: {
                    headline: this.localize.term('profiling_errorDisablingProfilerTitle'),
                    message: this.localize.term('profiling_errorDisablingProfilerDescription'),
                },
            });
            return;
        }
        this.#setToggle(false);
    }
    async #enableProfilingStatus() {
        this._isLoading = true;
        const { error } = await tryExecute(this, ProfilingService.putProfilingStatus({ body: { enabled: true } }));
        if (error) {
            this.#setToggle(false);
            return;
        }
        // Test that it was actually enabled
        const status = await this.#getProfilingStatus();
        if (!status) {
            this.#setToggle(false);
            this._notificationContext?.peek('warning', {
                data: {
                    headline: this.localize.term('profiling_errorEnablingProfilerTitle'),
                    message: this.localize.term('profiling_errorEnablingProfilerDescription'),
                },
            });
            return;
        }
        this.#setToggle(true);
    }
    #renderProfilingStatus() {
        return this._isDebugMode
            ? html `
					<umb-localize key="profiling_performanceProfilingDescription"></umb-localize>

					<uui-toggle
						id="toggle"
						label=${this.localize.term('profiling_activateByDefault')}
						label-position="left"
						?checked="${this._profilingStatus}"
						?disabled="${this._isLoading}"
						@change="${() => this._profilingStatus ? this.#disableProfilingStatus() : this.#enableProfilingStatus()}"></uui-toggle>

					${when(this._isLoading, () => html `<uui-loader-circle></uui-loader-circle>`)}

					<h4>
						<umb-localize key="profiling_reminder"></umb-localize>
					</h4>

					<umb-localize key="profiling_reminderDescription"></umb-localize>
				`
            : html `<umb-localize key="profiling_profilerEnabledDescription"></umb-localize>`;
    }
    render() {
        return html `
			<uui-box headline=${this.localize.term('profiling_performanceProfiling')}>
				${typeof this._profilingStatus === 'undefined'
            ? html `<uui-loader></uui-loader>`
            : this.#renderProfilingStatus()}
			</uui-box>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: block;
				padding: var(--uui-size-layout-1);
			}

			uui-toggle {
				font-weight: bold;
			}

			h4 {
				margin-bottom: 0;
			}

			h4 + p {
				margin-top: 0;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDashboardPerformanceProfilingElement.prototype, "_profilingStatus", void 0);
__decorate([
    state()
], UmbDashboardPerformanceProfilingElement.prototype, "_isDebugMode", void 0);
__decorate([
    state()
], UmbDashboardPerformanceProfilingElement.prototype, "_isLoading", void 0);
__decorate([
    query('#toggle')
], UmbDashboardPerformanceProfilingElement.prototype, "_toggle", void 0);
__decorate([
    consumeContext({ context: UMB_NOTIFICATION_CONTEXT })
], UmbDashboardPerformanceProfilingElement.prototype, "_notificationContext", void 0);
UmbDashboardPerformanceProfilingElement = __decorate([
    customElement('umb-dashboard-performance-profiling')
], UmbDashboardPerformanceProfilingElement);
export { UmbDashboardPerformanceProfilingElement };
export default UmbDashboardPerformanceProfilingElement;
