'use strict';
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Controller } from '@hotwired/stimulus';
// @ts-ignore
import { getComponent } from '@symfony/ux-live-component';
const RETURNING_KEY = 'ferienpass.returningUser';
const METHOD_KEY = 'ferienpass.lastLoginMethod';
export default class default_1 extends Controller {
    constructor() {
        super(...arguments);
        // Kept as a plain property (not a Stimulus value) so it survives
        // LiveComponent re-renders — morphdom would reset a value-backed attribute.
        this.method = 'passkey';
        this.pwVisible = false;
    }
    initialize() {
        return __awaiter(this, void 0, void 0, function* () {
            if (!(this.element instanceof HTMLElement)) {
                return;
            }
            try {
                this.component = yield getComponent(this.element);
                this.component.on('render:finished', () => this.applyState());
            }
            catch (e) {
            }
        });
    }
    connect() {
        this.applyState();
    }
    usePasskey() {
        this.method = 'passkey';
        this.applyState();
        this.syncMethodModel();
    }
    usePassword() {
        this.method = 'password';
        this.applyState();
        this.syncMethodModel();
    }
    // Remember the chosen sign-in method for this browser so a returning
    // registrant is greeted with the matching login view next time (the reader
    // side lives in the cms-signin / cms-signin-login controllers). Fired on
    // form submit — like sendMagicLink, this is optimistic: we record the
    // intent at the point of the user's action. No name or e-mail is stored.
    remember() {
        try {
            localStorage.setItem(RETURNING_KEY, '1');
            localStorage.setItem(METHOD_KEY, this.method);
        }
        catch (e) {
        }
    }
    // The form is a LiveComponent: programmatically setting the hidden field's
    // value does not fire a change event, so the live data store keeps the old
    // value. Dispatch one so the submitted method matches the picker.
    syncMethodModel() {
        if (this.hasMethodInputTarget) {
            this.methodInputTarget.dispatchEvent(new Event('change', { bubbles: true }));
        }
    }
    togglePassword() {
        this.pwVisible = !this.pwVisible;
        this.applyState();
    }
    applyState() {
        const passkey = 'passkey' === this.method;
        if (this.hasMethodInputTarget) {
            this.methodInputTarget.value = this.method;
        }
        if (this.hasPasskeyButtonTarget) {
            this.setSegment(this.passkeyButtonTarget, passkey);
            this.setSegment(this.passwordButtonTarget, !passkey);
        }
        this.toggle(this.hasPasswordFieldTarget ? this.passwordFieldTarget : null, !passkey);
        this.toggle(this.hasPasskeyInfoTarget ? this.passkeyInfoTarget : null, passkey);
        this.toggle(this.hasSubmitPasswordTarget ? this.submitPasswordTarget : null, !passkey);
        this.toggle(this.hasSubmitPasskeyTarget ? this.submitPasskeyTarget : null, passkey);
        if (this.hasPasswordTarget) {
            this.passwordTarget.type = this.pwVisible ? 'text' : 'password';
        }
        this.toggle(this.hasPasswordEyeTarget ? this.passwordEyeTarget : null, !this.pwVisible);
        this.toggle(this.hasPasswordEyeOffTarget ? this.passwordEyeOffTarget : null, this.pwVisible);
    }
    toggle(el, visible) {
        if (el) {
            el.style.display = visible ? '' : 'none';
        }
    }
    setSegment(el, active) {
        el.setAttribute('aria-checked', active ? 'true' : 'false');
        ['bg-white', 'text-gray-900', 'shadow-sm', 'dark:bg-gray-800', 'dark:text-gray-100'].forEach((c) => el.classList.toggle(c, active));
        ['text-gray-500', 'dark:text-gray-300'].forEach((c) => el.classList.toggle(c, !active));
    }
}
default_1.targets = [
    "methodInput", "passkeyButton", "passwordButton",
    "passwordField", "password", "passkeyInfo",
    "submitPassword", "submitPasskey", "passwordEye", "passwordEyeOff",
];
