import dayjs from "dayjs";
import { LitElement, css, html } from "lit";
import { customElement, property, state } from "lit/decorators.js";
import { when } from "lit/directives/when.js";
import { renderButton } from "../../common/button.js";
import { bindingGuard } from "../../../decorators/binding-guard.js";
import { bindings } from "../../../decorators/bindings.js";
import { withTailwindStyles } from "../../../decorators/with-tailwind-styles.js";
import flag from "../../../images/flag.js";
import three_dots from "../../../images/three-dots.js";
import { renderUserAction } from "./user-action.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 AtomicInsightUserActionsSession extends LitElement {
    get caseCreationIndex() {
        return this.userActions.findIndex(({ actionType })=>'TICKET_CREATION' === actionType);
    }
    get isCaseCreationSession() {
        return -1 !== this.caseCreationIndex;
    }
    initialize() {}
    connectedCallback() {
        super.connectedCallback();
        this.prepareUserActionsToDisplay();
    }
    willUpdate(changedProperties) {
        if (changedProperties.has('userActions')) this.prepareUserActionsToDisplay();
    }
    prepareUserActionsToDisplay() {
        if (this.isCaseCreationSession) {
            this.userActionsToDisplay = this.userActions.slice(this.caseCreationIndex);
            this.userActionsAfterCaseCreation = this.userActions.slice(0, this.caseCreationIndex);
        } else {
            this.userActionsToDisplay = this.userActions;
            this.userActionsAfterCaseCreation = [];
        }
    }
    showActionsAfterCaseCreation() {
        this.areActionsAfterCaseCreationVisible = true;
    }
    renderSessionStartDate() {
        const date = dayjs(this.startTimestamp);
        const formatedStartDate = date.format('ddd. MMMM D, YYYY');
        return html`
      <div
        data-testid="session-start-date-container"
        class="flex items-center px-2 pb-3"
      >
        ${when(this.isCaseCreationSession, ()=>html`
            <div
              part="session-start-icon__container"
              class="session-start-icon__container mr-2 flex h-5 w-5 items-center justify-center rounded-full"
            >
              <atomic-icon icon=${flag} class="h-3 w-3"></atomic-icon>
            </div>
          `)}
        <div class="flex-1 font-semibold">${formatedStartDate}</div>
      </div>
    `;
    }
    renderActions(actions) {
        return html`
      <ol class="px-3">
        ${actions?.map((action)=>renderUserAction({
                props: {
                    action,
                    bindings: this.bindings
                }
            }))}
      </ol>
    `;
    }
    renderShowMoreActionsButton() {
        const btnClasses = 'flex items-center p-1 max-w-full';
        const label = this.bindings.i18n.t('more-actions-in-session', {
            count: this.userActionsAfterCaseCreation.length
        });
        return html`
      <div
        data-testid="show-more-actions-button"
        class="flex items-center px-3 pb-3"
      >
        <div class="flex justify-center pr-2">
          <atomic-icon icon=${three_dots} class="h-3 w-3"></atomic-icon>
        </div>
        <div class="flex-1">
          ${renderButton({
            props: {
                style: 'text-primary',
                part: 'show-more-actions-button',
                class: btnClasses,
                ariaLabel: label,
                onClick: this.showActionsAfterCaseCreation.bind(this)
            }
        })(html`<span class="truncate text-xs font-light">${label}</span>`)}
        </div>
      </div>
    `;
    }
    render() {
        const isShowMoreActionsButtonVisible = !this.areActionsAfterCaseCreationVisible && !!this.userActionsAfterCaseCreation.length;
        return html`
      <div>
        ${this.renderSessionStartDate()}
        ${when(this.areActionsAfterCaseCreationVisible, ()=>html`
            <div data-testid="more-actions-section">
              ${this.renderActions(this.userActionsAfterCaseCreation)}
            </div>
          `)}
        ${when(isShowMoreActionsButtonVisible, ()=>this.renderShowMoreActionsButton())}
        ${this.renderActions(this.userActionsToDisplay)}
      </div>
    `;
    }
    constructor(...args){
        super(...args), this.userActions = [], this.userActionsToDisplay = [], this.userActionsAfterCaseCreation = [], this.areActionsAfterCaseCreationVisible = false;
    }
}
AtomicInsightUserActionsSession.styles = css`/*! tailwindcss v4.3.0 | MIT License | https://tailwindcss.com */.session-start-icon__container{background-color:#fbb439}.user-action__separator{background-color:var(--atomic-neutral)}.ticket-creation-action__text{color:var(--atomic-success)}.text-xxs{font-size:.625rem}`;
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof InsightBindings ? Object : InsightBindings)
], AtomicInsightUserActionsSession.prototype, "bindings", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Error ? Object : Error)
], AtomicInsightUserActionsSession.prototype, "error", void 0);
_ts_decorate([
    property({
        type: Number,
        attribute: 'start-timestamp'
    }),
    _ts_metadata("design:type", Number)
], AtomicInsightUserActionsSession.prototype, "startTimestamp", void 0);
_ts_decorate([
    property({
        type: Array,
        attribute: 'user-actions'
    }),
    _ts_metadata("design:type", "u" < typeof Array ? Object : Array)
], AtomicInsightUserActionsSession.prototype, "userActions", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Array ? Object : Array)
], AtomicInsightUserActionsSession.prototype, "userActionsToDisplay", void 0);
_ts_decorate([
    state(),
    _ts_metadata("design:type", "u" < typeof Array ? Object : Array)
], AtomicInsightUserActionsSession.prototype, "userActionsAfterCaseCreation", void 0);
_ts_decorate([
    state()
], AtomicInsightUserActionsSession.prototype, "areActionsAfterCaseCreationVisible", void 0);
_ts_decorate([
    bindingGuard(),
    _ts_metadata("design:type", Function),
    _ts_metadata("design:paramtypes", []),
    _ts_metadata("design:returntype", void 0)
], AtomicInsightUserActionsSession.prototype, "render", null);
AtomicInsightUserActionsSession = _ts_decorate([
    customElement('atomic-insight-user-actions-session'),
    bindings(),
    withTailwindStyles
], AtomicInsightUserActionsSession);
export { AtomicInsightUserActionsSession };
