'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';
/**
 * Consent management page: toggles the withdraw confirm sub-step in place of
 * the action buttons — purely client-side, the actual withdrawal is a live
 * action on the confirm button.
 */
export default class extends Controller {
    constructor() {
        super(...arguments);
        this.comp = null;
    }
    initialize() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                this.comp = yield getComponent(this.element);
                // The Live Component protects external DOM mutations (our `hidden`
                // toggles) from being morphed away — reset to the default state
                // after each render, the correct one once a confirm step was
                // executed or has become obsolete.
                this.comp.on('render:finished', () => this.reset());
            }
            catch (_a) {
                this.comp = null;
            }
        });
    }
    ask(event) {
        this.toggle(event, true);
    }
    cancel(event) {
        this.toggle(event, false);
    }
    reset() {
        this.element.querySelectorAll('[data-manage]').forEach((manage) => {
            var _a, _b;
            (_a = manage.querySelector('[data-actions]')) === null || _a === void 0 ? void 0 : _a.classList.remove('hidden');
            (_b = manage.querySelector('[data-confirm]')) === null || _b === void 0 ? void 0 : _b.classList.add('hidden');
        });
    }
    toggle(event, confirming) {
        var _a, _b;
        const manage = event.currentTarget.closest('[data-manage]');
        if (!manage) {
            return;
        }
        (_a = manage.querySelector('[data-actions]')) === null || _a === void 0 ? void 0 : _a.classList.toggle('hidden', confirming);
        (_b = manage.querySelector('[data-confirm]')) === null || _b === void 0 ? void 0 : _b.classList.toggle('hidden', !confirming);
    }
}
