import { __esDecorate, __runInitializers } from "tslib";
import { LitElement, html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';
import { bound } from '../decorators/bound.js';
import './help_panel.js';
/**
 * The main coordinator for the help system.
 * This element should be placed once in your application root and will:
 * - Listen for help requests from anywhere in the DOM
 * - Show/hide the help panel
 * - Coordinate between the help service and UI components
 *
 * It relies on the `HelpServiceController` to fetch and manage help topics.
 * Make sure to configure the `helpService` property with an instance of `HelpServiceController`.
 *
 * Usage:
 * ```ts
 * const helpServiceInstance = new HelpServiceController({
 *   categories: {
 *     property: '/help/property.yaml',
 *     entity: '/help/entity.yaml',
 *   },
 *   version: '1.0.0',
 * })
 * ```
 * ```html
 * <help-coordinator .helpService="${helpServiceInstance}"></help-coordinator>
 * ```
 */
let HelpCoordinator = (() => {
    let _classDecorators = [customElement('help-coordinator')];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    let _classSuper = LitElement;
    let _instanceExtraInitializers = [];
    let _category_decorators;
    let _category_initializers = [];
    let _category_extraInitializers = [];
    let _helpId_decorators;
    let _helpId_initializers = [];
    let _helpId_extraInitializers = [];
    let _topic_decorators;
    let _topic_initializers = [];
    let _topic_extraInitializers = [];
    let _opened_decorators;
    let _opened_initializers = [];
    let _opened_extraInitializers = [];
    let _trigger_decorators;
    let _trigger_initializers = [];
    let _trigger_extraInitializers = [];
    let _helpService_decorators;
    let _helpService_initializers = [];
    let _helpService_extraInitializers = [];
    let _handleHelpRequest_decorators;
    var HelpCoordinator = class extends _classSuper {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(_classSuper[Symbol.metadata] ?? null) : void 0;
            _category_decorators = [property({ type: String })];
            _helpId_decorators = [property({ type: String })];
            _topic_decorators = [property({ type: Object })];
            _opened_decorators = [state()];
            _trigger_decorators = [state()];
            _helpService_decorators = [property({ type: Object })];
            _handleHelpRequest_decorators = [bound];
            __esDecorate(this, null, _category_decorators, { kind: "accessor", name: "category", static: false, private: false, access: { has: obj => "category" in obj, get: obj => obj.category, set: (obj, value) => { obj.category = value; } }, metadata: _metadata }, _category_initializers, _category_extraInitializers);
            __esDecorate(this, null, _helpId_decorators, { kind: "accessor", name: "helpId", static: false, private: false, access: { has: obj => "helpId" in obj, get: obj => obj.helpId, set: (obj, value) => { obj.helpId = value; } }, metadata: _metadata }, _helpId_initializers, _helpId_extraInitializers);
            __esDecorate(this, null, _topic_decorators, { kind: "accessor", name: "topic", static: false, private: false, access: { has: obj => "topic" in obj, get: obj => obj.topic, set: (obj, value) => { obj.topic = value; } }, metadata: _metadata }, _topic_initializers, _topic_extraInitializers);
            __esDecorate(this, null, _opened_decorators, { kind: "accessor", name: "opened", static: false, private: false, access: { has: obj => "opened" in obj, get: obj => obj.opened, set: (obj, value) => { obj.opened = value; } }, metadata: _metadata }, _opened_initializers, _opened_extraInitializers);
            __esDecorate(this, null, _trigger_decorators, { kind: "accessor", name: "trigger", static: false, private: false, access: { has: obj => "trigger" in obj, get: obj => obj.trigger, set: (obj, value) => { obj.trigger = value; } }, metadata: _metadata }, _trigger_initializers, _trigger_extraInitializers);
            __esDecorate(this, null, _helpService_decorators, { kind: "accessor", name: "helpService", static: false, private: false, access: { has: obj => "helpService" in obj, get: obj => obj.helpService, set: (obj, value) => { obj.helpService = value; } }, metadata: _metadata }, _helpService_initializers, _helpService_extraInitializers);
            __esDecorate(this, null, _handleHelpRequest_decorators, { kind: "method", name: "handleHelpRequest", static: false, private: false, access: { has: obj => "handleHelpRequest" in obj, get: obj => obj.handleHelpRequest }, metadata: _metadata }, null, _instanceExtraInitializers);
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            HelpCoordinator = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            __runInitializers(_classThis, _classExtraInitializers);
        }
        #category_accessor_storage = (__runInitializers(this, _instanceExtraInitializers), __runInitializers(this, _category_initializers, void 0));
        /**
         * The help category of the current help request.
         * @attribute
         */
        get category() { return this.#category_accessor_storage; }
        set category(value) { this.#category_accessor_storage = value; }
        #helpId_accessor_storage = (__runInitializers(this, _category_extraInitializers), __runInitializers(this, _helpId_initializers, void 0));
        /**
         * The help ID of the current help request.
         * @attribute
         */
        get helpId() { return this.#helpId_accessor_storage; }
        set helpId(value) { this.#helpId_accessor_storage = value; }
        #topic_accessor_storage = (__runInitializers(this, _helpId_extraInitializers), __runInitializers(this, _topic_initializers, void 0));
        get topic() { return this.#topic_accessor_storage; }
        set topic(value) { this.#topic_accessor_storage = value; }
        #opened_accessor_storage = (__runInitializers(this, _topic_extraInitializers), __runInitializers(this, _opened_initializers, false
        /**
         * The element that triggered the help request.
         * This is used to position the help panel relative to the triggering element.
         * Might not always be set, depending on how the help request is triggered.
         */
        ));
        get opened() { return this.#opened_accessor_storage; }
        set opened(value) { this.#opened_accessor_storage = value; }
        #trigger_accessor_storage = (__runInitializers(this, _opened_extraInitializers), __runInitializers(this, _trigger_initializers, void 0));
        /**
         * The element that triggered the help request.
         * This is used to position the help panel relative to the triggering element.
         * Might not always be set, depending on how the help request is triggered.
         */
        get trigger() { return this.#trigger_accessor_storage; }
        set trigger(value) { this.#trigger_accessor_storage = value; }
        #helpService_accessor_storage = (__runInitializers(this, _trigger_extraInitializers), __runInitializers(this, _helpService_initializers, void 0));
        /**
         * The help service controller instance.
         * This should be configured with the appropriate categories and version.
         */
        get helpService() { return this.#helpService_accessor_storage; }
        set helpService(value) { this.#helpService_accessor_storage = value; }
        connectedCallback() {
            super.connectedCallback();
            // Listen for help requests from anywhere in the application
            document.addEventListener('request-help', this.handleHelpRequest);
            // Note: Escape key handling is now automatic with popover API
        }
        disconnectedCallback() {
            super.disconnectedCallback();
            document.removeEventListener('request-help', this.handleHelpRequest);
        }
        async handleHelpRequest(event) {
            if (event.defaultPrevented) {
                return;
            }
            const service = this.helpService;
            if (!service) {
                // eslint-disable-next-line no-console
                console.warn('Help service not configured');
                return;
            }
            const customEvent = event;
            const { category, id, target } = customEvent.detail;
            this.requestTopic(category, id, target);
            // Prevent further propagation to avoid multiple handlers
            event.stopPropagation();
        }
        async requestTopic(category, id, trigger) {
            const service = this.helpService;
            if (!service) {
                // eslint-disable-next-line no-console
                console.warn('Help service not configured');
                return;
            }
            if (!service.initialized) {
                await service.untilReady;
            }
            const topic = await service.getTopic(category, id);
            if (topic) {
                this.topic = topic;
                this.opened = true;
                this.trigger = trigger;
            }
            else {
                this.showHelpNotAvailable(category, id);
            }
            this.trackHelpUsage(category, id);
        }
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        trackHelpUsage(category, id) {
            // TODO: Implement analytics tracking here
        }
        showHelpNotAvailable(category, id) {
            this.topic = {
                id: 'not-found',
                category: 'general',
                title: 'Help Not Available',
                body: `Sorry, help content for **${category}::${id}** is not available yet.

Please check back later or contact support if you need assistance with this feature.`,
                links: [
                    { text: 'Contact Support', url: '/support' },
                    { text: 'Documentation', url: '/docs' },
                ],
            };
            this.opened = true;
        }
        handleClose() {
            this.opened = false;
        }
        render() {
            return html `<help-panel
      class="panel"
      .opened="${this.opened}"
      .topic="${this.topic}"
      .trigger="${this.trigger}"
      @close="${this.handleClose}"
    ></help-panel>`;
        }
        constructor() {
            super(...arguments);
            __runInitializers(this, _helpService_extraInitializers);
        }
    };
    return HelpCoordinator = _classThis;
})();
export { HelpCoordinator };
//# sourceMappingURL=help_coordinator.js.map