import * as i0 from '@angular/core';
import { signal, inject, computed, ViewEncapsulation, Component } from '@angular/core';
import * as i3 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i2 from '@acorex/components/button';
import { AXButtonModule } from '@acorex/components/button';
import { AXBasePageComponent } from '@acorex/components/page';
import * as i1 from '@acorex/components/decorators';
import { AXDecoratorModule } from '@acorex/components/decorators';
import { AXPActivityLogComponent } from '@acorex/platform/layout/components';
import { AXPVersioningService, AXVChangeType } from '@acorex/platform/common';
import * as i4 from '@acorex/core/translation';
import { AXTranslationModule } from '@acorex/core/translation';
import { AXPSystemActionType } from '@acorex/platform/core';

class AXMTimelineVersionHistoryPopupComponent extends AXBasePageComponent {
    constructor() {
        super(...arguments);
        this.isBusy = signal(false, ...(ngDevMode ? [{ debugName: "isBusy" }] : []));
        this.versioning = inject(AXPVersioningService);
        this.versions = signal([], ...(ngDevMode ? [{ debugName: "versions" }] : []));
        this.activities = computed(() => {
            const entries = this.versions();
            return entries.map((e) => ({
                id: e.id,
                title: e.summary ?? `${e.changeType} v${e.versionNumber}`,
                date: new Date(e.changedAt),
                user: { id: 'system', name: 'System', title: 'System' },
                reference: { id: this.refId, type: this.refType },
                changeType: mapChangeType(e.changeType),
                data: e.snapshotJson,
                changes: (e.patchFromPrevJson ?? []).slice(0, 10).map((c) => ({
                    type: mapOpToAction(c.op),
                    description: humanizeChange(c.path, c.op, this.fieldTitles),
                })),
            }));
        }, ...(ngDevMode ? [{ debugName: "activities" }] : []));
    }
    async ngOnInit() {
        this.isBusy.set(true);
        super.ngOnInit();
        const streamId = await this.versioning.getStreamIdIfExists(this.refType, this.refId);
        if (streamId) {
            const entries = await this.versioning.listEntries(streamId, { limit: this.limit, offset: this.offset });
            this.versions.set(entries);
        }
        else {
            this.versions.set([]);
        }
        this.isBusy.set(false);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXMTimelineVersionHistoryPopupComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "20.3.4", type: AXMTimelineVersionHistoryPopupComponent, isStandalone: true, selector: "axp-timeline-version-history-popup", providers: [], usesInheritance: true, ngImport: i0, template: "<ax-content>\n  <div class=\"ax-p-6 ax-min-h-[30vh] ax-overflow-y-auto\">\n    <axp-activity-log [activities]=\"activities()\" [loading]=\"isBusy()\"></axp-activity-log>\n  </div>\n</ax-content>\n<ax-footer>\n  <ax-prefix></ax-prefix>\n  <ax-suffix>\n    <ax-button\n      [text]=\"'@general:actions.close.title' | translate | async\"\n      look=\"solid\"\n      color=\"primary\"\n      (onClick)=\"close()\"\n    >\n    </ax-button>\n  </ax-suffix>\n</ax-footer>\n", dependencies: [{ kind: "ngmodule", type: 
                // Angular
                CommonModule }, { kind: "ngmodule", type: 
                // ACoreX
                AXDecoratorModule }, { kind: "component", type: i1.AXDecoratorGenericComponent, selector: "ax-footer, ax-header, ax-content, ax-divider, ax-form-hint, ax-prefix, ax-suffix, ax-text, ax-title, ax-subtitle, ax-placeholder, ax-overlay" }, { kind: "ngmodule", type: AXButtonModule }, { kind: "component", type: i2.AXButtonComponent, selector: "ax-button", inputs: ["disabled", "size", "tabIndex", "color", "look", "text", "toggleable", "selected", "iconOnly", "type", "loadingText"], outputs: ["onBlur", "onFocus", "onClick", "selectedChange", "toggleableChange", "lookChange", "colorChange", "disabledChange", "loadingTextChange"] }, { kind: "ngmodule", type: AXTranslationModule }, { kind: "component", type: 
                //
                AXPActivityLogComponent, selector: "axp-activity-log", inputs: ["activities", "loading"] }, { kind: "pipe", type: i3.AsyncPipe, name: "async" }, { kind: "pipe", type: i4.AXTranslatorPipe, name: "translate" }], encapsulation: i0.ViewEncapsulation.None }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.4", ngImport: i0, type: AXMTimelineVersionHistoryPopupComponent, decorators: [{
            type: Component,
            args: [{ selector: 'axp-timeline-version-history-popup', imports: [
                        // Angular
                        CommonModule,
                        // ACoreX
                        AXDecoratorModule,
                        AXButtonModule,
                        AXTranslationModule,
                        //
                        AXPActivityLogComponent
                    ], encapsulation: ViewEncapsulation.None, providers: [], template: "<ax-content>\n  <div class=\"ax-p-6 ax-min-h-[30vh] ax-overflow-y-auto\">\n    <axp-activity-log [activities]=\"activities()\" [loading]=\"isBusy()\"></axp-activity-log>\n  </div>\n</ax-content>\n<ax-footer>\n  <ax-prefix></ax-prefix>\n  <ax-suffix>\n    <ax-button\n      [text]=\"'@general:actions.close.title' | translate | async\"\n      look=\"solid\"\n      color=\"primary\"\n      (onClick)=\"close()\"\n    >\n    </ax-button>\n  </ax-suffix>\n</ax-footer>\n" }]
        }] });
function mapChangeType(t) {
    switch (t) {
        case AXVChangeType.Create:
            return AXPSystemActionType.Create;
        case AXVChangeType.Delete:
            return AXPSystemActionType.Delete;
        default:
            return AXPSystemActionType.Update;
    }
}
function mapOpToAction(op) {
    switch (op) {
        case 'add': return AXPSystemActionType.Create;
        case 'remove': return AXPSystemActionType.Delete;
        default: return AXPSystemActionType.Update;
    }
}
function humanizeChange(path, op, fieldTitles) {
    const key = (path.startsWith('/') ? path.slice(1) : path).split('/')[0] ?? '';
    const fieldTitle = fieldTitles?.[key] ?? key.replace(/([A-Z])/g, ' $1').trim();
    switch (op) {
        case 'add': return `${fieldTitle}: added`;
        case 'remove': return `${fieldTitle}: removed`;
        case 'rename': return `${fieldTitle}: renamed`;
        case 'metadata': return `${fieldTitle}: metadata updated`;
        case 'replace': return `${fieldTitle}: replaced`;
        default: return `${fieldTitle}: updated`;
    }
}

export { AXMTimelineVersionHistoryPopupComponent };
//# sourceMappingURL=acorex-modules-common-timeline-version-history-popup.component-BNTk1wM5.mjs.map
