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 { UmbUserKind } from '../../../../utils/index.js';
import { UMB_USER_WORKSPACE_CONTEXT } from '../../user-workspace.context-token.js';
import { html, customElement, state, ifDefined, css, nothing } from '../../../../../../../external/lit/index.js';
import { umbBindToValidation } from '../../../../../../core/validation/index.js';
import { UmbLitElement } from '../../../../../../core/lit-element/index.js';
let UmbUserWorkspaceProfileSettingsElement = class UmbUserWorkspaceProfileSettingsElement extends UmbLitElement {
    #userWorkspaceContext;
    constructor() {
        super();
        this._usernameIsEmail = true;
        this.consumeContext(UMB_USER_WORKSPACE_CONTEXT, (instance) => {
            this.#userWorkspaceContext = instance;
            this.observe(this.#userWorkspaceContext?.data, (user) => (this._user = user), 'umbUserObserver');
            this.observe(this.#userWorkspaceContext?.configRepository.part('usernameIsEmail'), (usernameIsEmail) => (this._usernameIsEmail = usernameIsEmail === undefined ? true : usernameIsEmail), 'umbUsernameIsEmailObserver');
        });
    }
    #onEmailChange(event) {
        const target = event.target;
        if (typeof target?.value === 'string') {
            this.#userWorkspaceContext?.updateProperty('email', target.value);
            if (this._usernameIsEmail) {
                this.#userWorkspaceContext?.updateProperty('userName', target.value);
            }
        }
    }
    #onUsernameChange(event) {
        const target = event.target;
        if (typeof target?.value === 'string') {
            this.#userWorkspaceContext?.updateProperty('userName', target.value);
        }
    }
    #onLanguageChange(event) {
        const target = event.target;
        if (typeof target?.value === 'string') {
            this.#userWorkspaceContext?.updateProperty('languageIsoCode', target.value);
        }
    }
    render() {
        return html `
			<uui-box>
				<div slot="headline"><umb-localize key="user_profile">Profile</umb-localize></div>
				${this.#renderEmailProperty()} ${this.#renderUsernameProperty()} ${this.#renderUILanguageProperty()}
			</uui-box>
		`;
    }
    #renderEmailProperty() {
        return html `
			<umb-property-layout
				mandatory
				label=${this.localize.term('general_email')}
				description=${this.localize.term('user_emailDescription', this._usernameIsEmail)}>
				<uui-input
					slot="editor"
					name="email"
					label=${this.localize.term('general_email')}
					required
					required-message=${this.localize.term('user_emailRequired')}
					value=${ifDefined(this._user?.email)}
					@change=${this.#onEmailChange}
					${umbBindToValidation(this)}>
				</uui-input>
			</umb-property-layout>
		`;
    }
    #renderUsernameProperty() {
        if (this._usernameIsEmail)
            return nothing;
        return html `
			<umb-property-layout
				mandatory
				label=${this.localize.term('user_loginname')}
				description=${this.localize.term('user_loginnameDescription')}>
				<uui-input
					slot="editor"
					name="username"
					autocomplete="off"
					label=${this.localize.term('user_loginname')}
					required
					required-message=${this.localize.term('user_loginnameRequired')}
					value=${ifDefined(this._user?.userName)}
					@change=${this.#onUsernameChange}>
				</uui-input>
			</umb-property-layout>
		`;
    }
    #renderUILanguageProperty() {
        if (this._user?.kind === UmbUserKind.API)
            return nothing;
        return html `
			<umb-property-layout
				label=${this.localize.term('user_language')}
				description=${this.localize.term('user_languageHelp')}>
				<umb-ui-culture-input
					slot="editor"
					name="language"
					label=${this.localize.term('user_language')}
					value=${ifDefined(this._user?.languageIsoCode ?? undefined)}
					@change=${this.#onLanguageChange}>
				</umb-ui-culture-input>
			</umb-property-layout>
		`;
    }
    static { this.styles = [
        css `
			:host {
				display: block;
			}

			uui-input {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbUserWorkspaceProfileSettingsElement.prototype, "_user", void 0);
__decorate([
    state()
], UmbUserWorkspaceProfileSettingsElement.prototype, "_usernameIsEmail", void 0);
UmbUserWorkspaceProfileSettingsElement = __decorate([
    customElement('umb-user-workspace-profile-settings')
], UmbUserWorkspaceProfileSettingsElement);
export { UmbUserWorkspaceProfileSettingsElement };
export default UmbUserWorkspaceProfileSettingsElement;
