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, customElement, html, query, state, when } from '../../../../external/lit/index.js';
import { UmbModalBaseElement } from '../../../core/modal/index.js';
import { UmbTextStyles } from '../../../core/style/index.js';
import { UmbUserItemRepository } from '../../user/index.js';
import { UMB_CURRENT_USER_CONTEXT } from '../../current-user/index.js';
let UmbChangePasswordModalElement = class UmbChangePasswordModalElement extends UmbModalBaseElement {
    #userItemRepository;
    #currentUserContext;
    #onClose() {
        this.modalContext?.reject();
    }
    #onSubmit(e) {
        e.preventDefault();
        const form = e.target;
        if (!form)
            return;
        const isValid = form.checkValidity();
        if (!isValid)
            return;
        const formData = new FormData(form);
        const oldPassword = formData.get('oldPassword');
        const newPassword = formData.get('newPassword');
        this.value = { oldPassword, newPassword };
        this.modalContext?.submit();
    }
    constructor() {
        super();
        this._headline = this.localize.term('general_changePassword');
        this._isCurrentUser = false;
        this.#userItemRepository = new UmbUserItemRepository(this);
        this.consumeContext(UMB_CURRENT_USER_CONTEXT, (instance) => {
            this.#currentUserContext = instance;
            this.#setIsCurrentUser();
        });
    }
    async #setIsCurrentUser() {
        if (!this.#currentUserContext || !this.data?.user.unique) {
            this._isCurrentUser = false;
            return;
        }
        this._isCurrentUser = await this.#currentUserContext.isUserCurrentUser(this.data.user.unique);
    }
    async firstUpdated() {
        this._confirmPasswordInput?.addValidator('customError', () => this.localize.term('user_passwordMismatch'), () => this._confirmPasswordInput?.value !== this._newPasswordInput?.value);
        if (!this.data?.user.unique)
            return;
        const { data } = await this.#userItemRepository.requestItems([this.data.user.unique]);
        if (data) {
            const userName = data[0].name;
            this._headline = `Change password for ${userName}`;
        }
    }
    render() {
        return html `
			<uui-dialog-layout class="uui-text" headline=${this._headline}>
				<uui-form>
					<form id="ChangePasswordForm" @submit=${this.#onSubmit}>
						${when(this._isCurrentUser, () => html `
								<uui-form-layout-item style="margin-bottom: var(--uui-size-layout-2)">
									<uui-label slot="label" id="oldPasswordLabel" for="oldPassword" required>
										<umb-localize key="user_passwordCurrent">Current password</umb-localize>
									</uui-label>
									<uui-input-password
										id="oldPassword"
										name="oldPassword"
										required
										required-message="Current password is required">
									</uui-input-password>
								</uui-form-layout-item>
							`)}
						<uui-form-layout-item>
							<uui-label slot="label" id="newPasswordLabel" for="newPassword" required>
								<umb-localize key="user_newPassword">New password</umb-localize>
							</uui-label>
							<uui-input-password
								id="newPassword"
								name="newPassword"
								required
								required-message="New password is required">
							</uui-input-password>
						</uui-form-layout-item>
						<uui-form-layout-item>
							<uui-label slot="label" id="confirmPasswordLabel" for="confirmPassword" required>
								<umb-localize key="user_confirmNewPassword">Confirm new password</umb-localize>
							</uui-label>
							<uui-input-password
								id="confirmPassword"
								name="confirmPassword"
								required
								required-message="Confirm password is required">
							</uui-input-password>
						</uui-form-layout-item>
					</form>
				</uui-form>
				<uui-button slot="actions" label=${this.localize.term('general_cancel')} @click=${this.#onClose}></uui-button>
				<uui-button
					slot="actions"
					type="submit"
					form="ChangePasswordForm"
					color="positive"
					look="primary"
					label=${this.localize.term('general_confirm')}></uui-button>
			</uui-dialog-layout>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			uui-input-password {
				width: 100%;
			}
		`,
    ]; }
};
__decorate([
    query('#newPassword')
], UmbChangePasswordModalElement.prototype, "_newPasswordInput", void 0);
__decorate([
    query('#confirmPassword')
], UmbChangePasswordModalElement.prototype, "_confirmPasswordInput", void 0);
__decorate([
    state()
], UmbChangePasswordModalElement.prototype, "_headline", void 0);
__decorate([
    state()
], UmbChangePasswordModalElement.prototype, "_isCurrentUser", void 0);
UmbChangePasswordModalElement = __decorate([
    customElement('umb-change-password-modal')
], UmbChangePasswordModalElement);
export { UmbChangePasswordModalElement };
export default UmbChangePasswordModalElement;
