import { __decorate } from "tslib";
import { attr, customElement, GenesisElement, html, observable } from '@genesislcap/web-core';
import { agenticActivityBus } from '../../channel/ai-activity-bus';
const DEFAULT_MIN_SHOW_TIME = 600;
/**
 * Wrapper component that shows an `<ai-halo-overlay>` when the AI assistant
 * is executing one of the declared tool names.
 *
 * @example
 * ```html
 * <ai-activity-halo tools="search_trades">
 *   <trades-grid></trades-grid>
 * </ai-activity-halo>
 * ```
 *
 * @beta
 */
let AiActivityHalo = class AiActivityHalo extends GenesisElement {
    constructor() {
        super(...arguments);
        /** Comma-separated list of tool names this element reacts to. */
        this.tools = '';
        /** Minimum time in ms the halo stays visible after activation. */
        this.minShowTime = DEFAULT_MIN_SHOW_TIME;
        /** @internal */
        this.haloActive = false;
        this.startTime = 0;
    }
    connectedCallback() {
        super.connectedCallback();
        const unsubStart = agenticActivityBus.subscribe('halo-start', ({ toolNames }) => {
            const myTools = this.tools
                .split(',')
                .map((t) => t.trim())
                .filter(Boolean);
            if (myTools.length === 0 || toolNames.some((name) => myTools.includes(name))) {
                this.activate();
            }
        });
        const unsubStop = agenticActivityBus.subscribe('halo-stop', () => {
            this.scheduleDeactivate();
        });
        this.unsubBus = () => {
            unsubStart();
            unsubStop();
        };
    }
    disconnectedCallback() {
        var _a;
        super.disconnectedCallback();
        (_a = this.unsubBus) === null || _a === void 0 ? void 0 : _a.call(this);
        this.unsubBus = undefined;
        if (this.hideTimeout !== undefined) {
            clearTimeout(this.hideTimeout);
            this.hideTimeout = undefined;
        }
    }
    activate() {
        if (this.hideTimeout !== undefined) {
            clearTimeout(this.hideTimeout);
            this.hideTimeout = undefined;
        }
        this.startTime = Date.now();
        this.haloActive = true;
    }
    scheduleDeactivate() {
        const elapsed = Date.now() - this.startTime;
        const remaining = this.minShowTime - elapsed;
        if (remaining <= 0) {
            this.haloActive = false;
        }
        else {
            this.hideTimeout = setTimeout(() => {
                this.haloActive = false;
                this.hideTimeout = undefined;
            }, remaining);
        }
    }
};
__decorate([
    attr
], AiActivityHalo.prototype, "tools", void 0);
__decorate([
    attr({ attribute: 'min-show-time', converter: { fromView: Number, toView: String } })
], AiActivityHalo.prototype, "minShowTime", void 0);
__decorate([
    observable
], AiActivityHalo.prototype, "haloActive", void 0);
AiActivityHalo = __decorate([
    customElement({
        name: 'ai-activity-halo',
        template: html `
    <div class="wrapper">
      <slot></slot>
      <ai-halo-overlay aria-hidden="true" ?active="${(x) => x.haloActive}"></ai-halo-overlay>
    </div>
  `,
        styles: `
    :host {
      display: contents;
    }
    .wrapper {
      position: relative;
      overflow: hidden;
      border-radius: inherit;
      height: 100%;
      width: 100%;
    }
  `,
    })
], AiActivityHalo);
export { AiActivityHalo };
