import { ArrayValue, Schema, StringValue } from "@coveo/bueno";
import { buildUserActions } from "@coveo/headless/insight";
import { LitElement, css, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { renderButton } from "../../common/button.js";
import { renderNoItemsContainer } from "../../common/no-items/container.js";
import { renderMagnifyingGlass } from "../../common/no-items/magnifying-glass.js";
import { ValidatePropsController } from "../../common/validate-props-controller/validate-props-controller.js";
import { bindStateToController } from "../../../decorators/bind-state.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import big_arrow_down from "../../../images/big-arrow-down.js";
import big_arrow_up from "../../../images/big-arrow-up.js";
import "../../common/atomic-icon/atomic-icon.js";
import "../atomic-insight-user-actions-session/atomic-insight-user-actions-session.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);
}
class AtomicInsightUserActionsTimeline extends LitElement {
    initialize() {
        this.userActions = buildUserActions(this.bindings.engine, {
            options: {
                ticketCreationDate: this.ticketCreationDateTime,
                excludedCustomActions: this.excludedCustomActions
            }
        });
        this.userActions.fetchUserActions(this.userId);
    }
    toggleFollowingSessions() {
        this.followingSessionsAreVisible = !this.followingSessionsAreVisible;
    }
    togglePrecedingSessions() {
        this.precedingSessionsAreVisible = !this.precedingSessionsAreVisible;
    }
    renderSessions(sessions, renderSeparator, testId) {
        if (!sessions) return nothing;
        return html`
      <div>
        ${sessions.map(({ actions, start })=>html`
            <div class="mt-4">
              <atomic-insight-user-actions-session
                .startTimestamp=${start}
                .userActions=${actions}
                data-testid=${testId}
              ></atomic-insight-user-actions-session>
            </div>
            ${renderSeparator ? renderSeparator() : nothing}
          `)}
      </div>
    `;
    }
    renderToggleFollowingSessionsButton() {
        const btnClasses = 'flex items-center text-left p-2 text-sm max-w-full';
        const iconClasses = 'h-3 w-3 mr-1';
        const label = this.followingSessionsAreVisible ? this.bindings.i18n.t('hide-following-sessions') : this.bindings.i18n.t('show-following-sessions');
        const icon = this.followingSessionsAreVisible ? big_arrow_down : big_arrow_up;
        return html`
      <div class="flex justify-center p-2">
        ${renderButton({
            props: {
                style: 'text-primary',
                part: 'toggle-following-sessions',
                class: btnClasses,
                ariaLabel: label,
                onClick: this.toggleFollowingSessions.bind(this)
            }
        })(html`
          <atomic-icon
            part="toggle-following-sessions-icon"
            class=${iconClasses}
            .icon=${icon}
          ></atomic-icon>
          <span class="truncate">${label}</span>
        `)}
      </div>
    `;
    }
    renderTogglePrecedingSessionsButton() {
        const btnClasses = 'flex items-center text-left p-2 text-sm max-w-full';
        const iconClasses = 'h-3 w-3 mr-1';
        const label = this.precedingSessionsAreVisible ? this.bindings.i18n.t('hide-preceding-sessions') : this.bindings.i18n.t('show-preceding-sessions');
        const icon = this.precedingSessionsAreVisible ? big_arrow_up : big_arrow_down;
        return html`
      <div class="flex justify-center p-2">
        ${renderButton({
            props: {
                style: 'text-primary',
                part: 'toggle-preceding-sessions',
                class: btnClasses,
                ariaLabel: label,
                onClick: this.togglePrecedingSessions.bind(this)
            }
        })(html`
          <atomic-icon
            part="toggle-preceding-sessions-icon"
            class=${iconClasses}
            .icon=${icon}
          ></atomic-icon>
          <span class="truncate">${label}</span>
        `)}
      </div>
    `;
    }
    renderFollowingSessionsSection() {
        if (!this.userActionsState.timeline?.followingSessions?.length) return nothing;
        return html`
      ${this.renderToggleFollowingSessionsButton()}
      <div class="separator mx-1 rounded"></div>
      ${when(this.followingSessionsAreVisible, ()=>this.renderSessions(this.userActionsState.timeline?.followingSessions, ()=>html`<div class="separator mx-1 mt-4 rounded"></div>`, 'following-session'))}
    `;
    }
    renderPrecedingSessionsSection() {
        if (!this.userActionsState.timeline?.precedingSessions?.length) return nothing;
        return html`
      <div class="separator mx-1 mt-4 rounded"></div>
      ${when(this.precedingSessionsAreVisible, ()=>this.renderSessions(this.userActionsState.timeline?.precedingSessions, ()=>html`<div class="separator mx-1 mt-4 rounded"></div>`, 'preceding-session'))}
      ${this.renderTogglePrecedingSessionsButton()}
    `;
    }
    renderTimeline() {
        const session = this.userActionsState.timeline?.session;
        if (!session) return nothing;
        return html`
      <div>
        ${this.renderFollowingSessionsSection()}
        <div class="mt-4">
          <atomic-insight-user-actions-session
            .startTimestamp=${session.start}
            .userActions=${session.actions}
            data-testid="active-session"
          ></atomic-insight-user-actions-session>
        </div>
        ${this.renderPrecedingSessionsSection()}
      </div>
    `;
    }
    renderNoUserActionsScreen() {
        return html`
      <div class="my-6 py-3" data-testid="user-actions-error">
        ${renderNoItemsContainer()(html`
          ${renderMagnifyingGlass()}
          <div class="my-2 max-w-full text-center text-2xl font-light">
            ${this.bindings.i18n.t('no-user-actions-available')}
          </div>
          <div class="text-neutral-dark my-2 text-center text-lg">
            ${this.bindings.i18n.t('no-user-actions-associated-with-params')}
          </div>
        `)}
      </div>
    `;
    }
    render() {
        const areUserActionsAvailable = this.userActionsState?.timeline?.session;
        const hasError = this.userActionsState?.error;
        if (areUserActionsAvailable && !hasError) return this.renderTimeline();
        return this.renderNoUserActionsScreen();
    }
    constructor(){
        super(), this.excludedCustomActions = [], this.followingSessionsAreVisible = false, this.precedingSessionsAreVisible = false;
        new ValidatePropsController(this, ()=>({
                userId: this.userId,
                ticketCreationDateTime: this.ticketCreationDateTime,
                excludedCustomActions: this.excludedCustomActions
            }), AtomicInsightUserActionsTimeline.propsSchema, false);
    }
}
AtomicInsightUserActionsTimeline.styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */.separator{background-color:var(--atomic-insight-user-actions-timeline-separator-background);height:1px}`;
AtomicInsightUserActionsTimeline.propsSchema = new Schema({
    userId: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    ticketCreationDateTime: new StringValue({
        required: true,
        emptyAllowed: false
    }),
    excludedCustomActions: new ArrayValue({
        each: new StringValue({}),
        required: false
    })
});
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightUserActionsTimeline.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightUserActionsTimeline.prototype, "error", void 0);
_ts_decorate([
    bindStateToController('userActions'),
    state(),
    _ts_metadata("design:type", "u" < typeof InsightUserActionsState ? Object : InsightUserActionsState)
], AtomicInsightUserActionsTimeline.prototype, "userActionsState", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'user-id'
    }),
    _ts_metadata("design:type", String)
], AtomicInsightUserActionsTimeline.prototype, "userId", void 0);
_ts_decorate([
    property({
        type: String,
        attribute: 'ticket-creation-date-time'
    }),
    _ts_metadata("design:type", String)
], AtomicInsightUserActionsTimeline.prototype, "ticketCreationDateTime", void 0);
_ts_decorate([
    property({
        type: Array,
        attribute: 'excluded-custom-actions'
    }),
    _ts_metadata("design:type", Array)
], AtomicInsightUserActionsTimeline.prototype, "excludedCustomActions", void 0);
_ts_decorate([
    state()
], AtomicInsightUserActionsTimeline.prototype, "followingSessionsAreVisible", void 0);
_ts_decorate([
    state()
], AtomicInsightUserActionsTimeline.prototype, "precedingSessionsAreVisible", void 0);
_ts_decorate([
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightUserActionsTimeline.prototype, "render", null);
AtomicInsightUserActionsTimeline = _ts_decorate([
    customElement('atomic-insight-user-actions-timeline'),
    bindings(),
    withTailwindStyles,
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", [])
], AtomicInsightUserActionsTimeline);
export { AtomicInsightUserActionsTimeline };
