import { LitElement, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { map } from "lit/directives/map.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import atomic_agent_stream_of_thought_tw_css from "./atomic-agent-stream-of-thought.tw.css.js";
function _ts_decorate(decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) 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;
}
function _ts_metadata(k, v) {
    if ("object" == typeof Reflect && "function" == typeof Reflect.metadata) return Reflect.metadata(k, v);
}
const stepLabelKeys = {
    'thinking-before-search': {
        active: 'agent-generation-step-analyzing-question',
        completed: 'agent-generation-step-analyzing-question-completed'
    },
    searching: {
        active: 'agent-generation-step-search',
        completed: 'agent-generation-step-search-completed'
    },
    'thinking-after-search': {
        active: 'agent-generation-step-analyzing-results',
        completed: 'agent-generation-step-analyzing-results-completed'
    },
    answering: {
        active: 'agent-generation-step-answering',
        completed: 'agent-generation-step-answering-completed'
    }
};
class AtomicAgentStreamOfThought extends LitElement {
    willUpdate(changed) {
        if (changed.has('isStreaming')) this.expanded = this.isStreaming;
    }
    render() {
        const resolvedSteps = resolveSteps(this.agentSteps);
        if (0 === resolvedSteps.length) return nothing;
        const isComplete = !this.isStreaming;
        const isCollapsible = resolvedSteps.length > 1;
        if (isComplete && !this.expanded && isCollapsible) return html`
        <div class="timeline">${this.renderCollapsedTimelineSummary()}</div>
      `;
        return html`
      <div class="timeline">
        ${map(resolvedSteps, (step)=>this.renderStep(step))}
        ${isComplete && isCollapsible ? this.renderToggleButton() : nothing}
      </div>
    `;
    }
    renderStep(step) {
        const labelKey = 'active' === step.status ? stepLabelKeys[step.type].active : stepLabelKeys[step.type].completed;
        const label = this.i18n.t(labelKey);
        return html`
      <div class="step">
        ${'active' === step.status ? this.renderSpinner() : this.renderCheckmark()}
        <span class="step-label">${label}</span>
      </div>
    `;
    }
    renderCollapsedTimelineSummary() {
        const resolvedSteps = resolveSteps(this.agentSteps);
        if (0 === resolvedSteps.length) return nothing;
        const step = resolvedSteps[resolvedSteps.length - 1];
        const labelKey = stepLabelKeys[step.type].completed;
        const label = this.i18n.t(labelKey);
        return html`
      <button
        class="collapsed-timeline-summary"
        @click=${this.handleToggle}
        aria-expanded=${this.expanded}
        type="button"
      >
        ${this.renderCheckmark()}
        <span class="step-label">${label}</span>
        ${this.renderChevron()}
      </button>
    `;
    }
    renderToggleButton() {
        return html`
      <button
        class="toggle-button"
        @click=${this.handleToggle}
        aria-expanded=${this.expanded}
        type="button"
      >
        <span class="toggle-label">${this.i18n.t('collapse')}</span>
        ${this.renderChevron()}
      </button>
    `;
    }
    renderSpinner() {
        return html`<span class="step-icon"
      ><span class="spinner" aria-hidden="true"></span
    ></span>`;
    }
    renderCheckmark() {
        return html`
      <span class="step-icon checkmark-icon">
        <svg
          width="12"
          height="9"
          viewBox="0 0 12 9"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
          aria-hidden="true"
        >
          <path
            d="M1 4.33333L4.33333 7.66667L11 1"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          />
        </svg>
      </span>
    `;
    }
    renderChevron() {
        return html`
      <span class=${classMap({
            chevron: true,
            'chevron-up': this.expanded
        })}>
        <svg
          width="11"
          height="6"
          viewBox="0 0 11 6"
          fill="none"
          xmlns="http://www.w3.org/2000/svg"
          aria-hidden="true"
        >
          <path
            d="M1.18998 0.52682L5.43262 4.76946L9.67526 0.526821"
            stroke="currentColor"
            stroke-linecap="round"
            stroke-linejoin="round"
          />
        </svg>
      </span>
    `;
    }
    handleToggle() {
        this.expanded = !this.expanded;
    }
    constructor(...args){
        super(...args), this.agentSteps = [], this.isStreaming = false, this.expanded = true;
    }
}
AtomicAgentStreamOfThought.styles = [
    atomic_agent_stream_of_thought_tw_css
];
_ts_decorate([
    property({
        attribute: false
    }),
    _ts_metadata("design:type", "u" < typeof i18n ? Object : i18n)
], AtomicAgentStreamOfThought.prototype, "i18n", void 0);
_ts_decorate([
    property({
        attribute: false
    }),
    _ts_metadata("design:type", Array)
], AtomicAgentStreamOfThought.prototype, "agentSteps", void 0);
_ts_decorate([
    property({
        attribute: false
    })
], AtomicAgentStreamOfThought.prototype, "isStreaming", void 0);
_ts_decorate([
    state()
], AtomicAgentStreamOfThought.prototype, "expanded", void 0);
AtomicAgentStreamOfThought = _ts_decorate([
    customElement('atomic-agent-stream-of-thought'),
    withTailwindStyles
], AtomicAgentStreamOfThought);
function resolveSteps(steps) {
    let searchWasPerformed = false;
    return steps.map((step)=>{
        let type;
        if ('searching' === step.name) {
            searchWasPerformed = true;
            type = 'searching';
        } else type = 'answering' === step.name ? 'answering' : searchWasPerformed ? 'thinking-after-search' : 'thinking-before-search';
        return {
            type,
            status: step.status
        };
    });
}
export { AtomicAgentStreamOfThought, resolveSteps };
