var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") 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;
};
import { UMB_CURRENT_USER_HISTORY_STORE_CONTEXT } from './current-user-history.store.token.js';
import { html, customElement, state, nothing, css, repeat } from '../../../../external/lit/index.js';
import { UmbLitElement } from '../../../core/lit-element/index.js';
import { UmbTextStyles } from '../../../core/style/index.js';
import { UmbPaginationManager } from '../../../core/utils/index.js';
const PAGE_SIZE = 10;
let UmbCurrentUserHistoryUserProfileAppElement = class UmbCurrentUserHistoryUserProfileAppElement extends UmbLitElement {
    #currentUserHistoryStore;
    #pagination;
    constructor() {
        super();
        this._history = [];
        this._currentPageNumber = 1;
        this._totalPages = 1;
        this.#pagination = new UmbPaginationManager();
        this.#pagination.setPageSize(PAGE_SIZE);
        this.observe(this.#pagination.currentPage, (number) => (this._currentPageNumber = number));
        this.observe(this.#pagination.totalPages, (number) => (this._totalPages = number));
        this.consumeContext(UMB_CURRENT_USER_HISTORY_STORE_CONTEXT, (instance) => {
            this.#currentUserHistoryStore = instance;
            this.#observeHistory();
        });
    }
    #observeHistory() {
        if (this.#currentUserHistoryStore) {
            this.observe(this.#currentUserHistoryStore.latestHistory, (history) => {
                this._history = history.reverse();
                this.#pagination.setTotalItems(this._history.length);
            }, 'umbCurrentUserHistoryObserver');
        }
    }
    #onPageChange(event) {
        this.#pagination.setCurrentPageNumber(event.target.current);
    }
    #getCurrentPageItems() {
        const skip = this.#pagination.getSkip();
        return this._history.slice(skip, skip + PAGE_SIZE);
    }
    #truncate(input, length, separator = '...') {
        if (!input)
            return '';
        if (input.length <= length)
            return input;
        const separatorLength = separator.length;
        const charsToShow = length - separatorLength;
        const frontChars = Math.ceil(charsToShow / 2);
        const backChars = Math.floor(charsToShow / 2);
        return input.substring(0, frontChars) + separator + input.substring(input.length - backChars);
    }
    render() {
        return html `
			<uui-box headline=${this.localize.term('user_yourHistory')}>
				<uui-ref-list>
					${repeat(this.#getCurrentPageItems(), (item) => item.unique, (item) => this.#renderItem(item))}
				</uui-ref-list>
				${this.#renderPagination()}
			</uui-box>
		`;
    }
    #renderItem(item) {
        const label = Array.isArray(item.label) ? item.label[0] : item.label;
        return html `
			<uui-ref-node name=${label} detail=${this.#truncate(item.displayPath, 50)} href=${item.path}>
				<uui-icon slot="icon" name="icon-link"></uui-icon>
			</uui-ref-node>
		`;
    }
    #renderPagination() {
        if (this._totalPages <= 1)
            return nothing;
        return html `
			<uui-pagination
				.current=${this._currentPageNumber}
				.total=${this._totalPages}
				firstlabel=${this.localize.term('general_first')}
				previouslabel=${this.localize.term('general_previous')}
				nextlabel=${this.localize.term('general_next')}
				lastlabel=${this.localize.term('general_last')}
				@change=${this.#onPageChange}></uui-pagination>
		`;
    }
    static { this.styles = [
        UmbTextStyles,
        css `
			uui-ref-node {
				padding-left: 0;
				padding-right: 0;
			}

			uui-pagination {
				display: flex;
				justify-content: center;
				margin-top: var(--uui-size-layout-1);
			}
		`,
    ]; }
};
__decorate([
    state()
], UmbCurrentUserHistoryUserProfileAppElement.prototype, "_history", void 0);
__decorate([
    state()
], UmbCurrentUserHistoryUserProfileAppElement.prototype, "_currentPageNumber", void 0);
__decorate([
    state()
], UmbCurrentUserHistoryUserProfileAppElement.prototype, "_totalPages", void 0);
UmbCurrentUserHistoryUserProfileAppElement = __decorate([
    customElement('umb-current-user-history-user-profile-app')
], UmbCurrentUserHistoryUserProfileAppElement);
export { UmbCurrentUserHistoryUserProfileAppElement };
export default UmbCurrentUserHistoryUserProfileAppElement;
