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 { css, html, customElement, state, unsafeHTML } from '../../external/lit/index.js';
import { TelemetryLevelModel, TelemetryService } from '../core/backend-api/index.js';
import { UmbLitElement } from '../core/lit-element/index.js';
import { tryExecute, UmbApiError } from '../core/resources/index.js';
import { UmbTextStyles } from '../core/style/index.js';
let UmbDashboardTelemetryElement = class UmbDashboardTelemetryElement extends UmbLitElement {
    constructor() {
        super();
        this._telemetryFormData = TelemetryLevelModel.BASIC;
        this._telemetryLevels = [];
        this._errorMessage = '';
        this._buttonState = undefined;
        this._handleSubmit = async (e) => {
            e.stopPropagation();
            this._buttonState = 'waiting';
            const { error } = await tryExecute(this, TelemetryService.postTelemetryLevel({
                body: { telemetryLevel: this._telemetryFormData },
            }));
            if (error) {
                this._buttonState = 'failed';
                this._errorMessage =
                    (UmbApiError.isUmbApiError(error) ? error.problemDetails.detail : error.message) ?? 'Unknown error';
                return;
            }
            this._buttonState = 'success';
        };
        this._setup();
    }
    async _setup() {
        const telemetryLevels = await tryExecute(this, TelemetryService.getTelemetry({ query: { skip: 0, take: 3 } }));
        this._telemetryLevels = telemetryLevels.data?.items ?? [];
        const telemetryLevel = await tryExecute(this, TelemetryService.getTelemetryLevel());
        this._telemetryFormData = telemetryLevel.data?.telemetryLevel ?? TelemetryLevelModel.BASIC;
    }
    _handleChange(e) {
        const target = e.target;
        this._telemetryFormData =
            this._telemetryLevels[parseInt(target.value) - 1].telemetryLevel ?? TelemetryLevelModel.BASIC;
    }
    get _selectedTelemetryIndex() {
        return this._telemetryLevels.findIndex((x) => x.telemetryLevel === this._telemetryFormData) ?? 0;
    }
    get _selectedTelemetry() {
        return this._telemetryLevels.find((x) => x.telemetryLevel === this._telemetryFormData) ?? this._telemetryLevels[1];
    }
    get _selectedTelemetryDescription() {
        switch (this._selectedTelemetry.telemetryLevel) {
            case TelemetryLevelModel.MINIMAL:
                return this.localize.term('analytics_minimalLevelDescription');
            case TelemetryLevelModel.BASIC:
                return this.localize.term('analytics_basicLevelDescription');
            case TelemetryLevelModel.DETAILED:
                return this.localize.term('analytics_detailedLevelDescription');
            default:
                return 'Could not find description for this setting';
        }
    }
    _renderSettingSlider() {
        if (!this._telemetryLevels || this._telemetryLevels.length < 1)
            return;
        return html `
			<uui-slider
				@input=${this._handleChange}
				name="telemetryLevel"
				label=${this.localize.term('analytics_consentForAnalytics')}
				value=${this._selectedTelemetryIndex + 1}
				min="1"
				max=${this._telemetryLevels.length}
				hide-step-values
				hide-value-label></uui-slider>
			<h3>${this._selectedTelemetry.telemetryLevel}</h3>
			<p>${unsafeHTML(this._selectedTelemetryDescription)}</p>
		`;
    }
    render() {
        return html `
			<uui-box headline=${this.localize.term('analytics_consentForAnalytics')}>
				<div style="max-width:75ch">
					<umb-localize key="analytics_analyticsDescription"></umb-localize>
					${this._renderSettingSlider()}
					<uui-button
						look="primary"
						color="positive"
						label=${this.localize.term('buttons_save')}
						@click="${this._handleSubmit}"
						.state=${this._buttonState}></uui-button>
				</div>
				${this._errorMessage ? html `<p class="error">${this._errorMessage}</p>` : ''}
			</uui-box>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			:host {
				display: block;
				padding: var(--uui-size-layout-1);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbDashboardTelemetryElement.prototype, "_telemetryFormData", void 0);
__decorate([
    state()
], UmbDashboardTelemetryElement.prototype, "_telemetryLevels", void 0);
__decorate([
    state()
], UmbDashboardTelemetryElement.prototype, "_errorMessage", void 0);
__decorate([
    state()
], UmbDashboardTelemetryElement.prototype, "_buttonState", void 0);
UmbDashboardTelemetryElement = __decorate([
    customElement('umb-dashboard-telemetry')
], UmbDashboardTelemetryElement);
export { UmbDashboardTelemetryElement };
export default UmbDashboardTelemetryElement;
