import * as i1 from '@acorex/components/button';
import { AXButtonModule } from '@acorex/components/button';
import { AXCheckBoxModule } from '@acorex/components/check-box';
import * as i2 from '@acorex/components/decorators';
import { AXDecoratorModule } from '@acorex/components/decorators';
import { AXLabelModule } from '@acorex/components/label';
import { AXBasePageComponent } from '@acorex/components/page';
import * as i5 from '@acorex/core/translation';
import { AXTranslationModule } from '@acorex/core/translation';
import * as i1$2 from '@angular/common';
import { CommonModule } from '@angular/common';
import * as i0 from '@angular/core';
import { inject, input, computed, linkedSignal, Component } from '@angular/core';
import * as i1$1 from '@acorex/platform/layout/widget-core';
import { AXPWidgetCoreModule } from '@acorex/platform/layout/widget-core';
import { AXPFileStorageService } from '@acorex/platform/common';

class AXPFileListPopupComponent extends AXBasePageComponent {
    constructor() {
        super(...arguments);
        this.fileService = inject(AXPFileStorageService);
        //#region ----   Component Properties   ----
        this.files = input([], ...(ngDevMode ? [{ debugName: "files" }] : []));
        this.readOnly = input(false, ...(ngDevMode ? [{ debugName: "readOnly" }] : []));
        this.multiple = input(false, ...(ngDevMode ? [{ debugName: "multiple" }] : []));
        this.fileEditable = input(true, ...(ngDevMode ? [{ debugName: "fileEditable" }] : []));
        this.accept = input('*', ...(ngDevMode ? [{ debugName: "accept" }] : []));
        this.maxFileSize = input(1024 * 1024 * 100, ...(ngDevMode ? [{ debugName: "maxFileSize" }] : []));
        //#endregion
        //#region ----   Computed Properties   ----
        this.mode = computed(() => this.readOnly() ? 'view' : 'edit', ...(ngDevMode ? [{ debugName: "mode" }] : []));
        //#endregion
        //#region ----   Context & Node Management   ----
        this.context = linkedSignal(() => ({}), ...(ngDevMode ? [{ debugName: "context" }] : []));
        this.node = computed(() => ({
            type: 'file-uploader',
            path: 'files',
            defaultValue: Array.isArray(this.files()) && typeof this.files()[0] !== 'string' ? this.files() : [],
            options: {
                readOnly: this.readOnly(),
                multiple: this.multiple(),
                accept: this.accept(),
                maxFileSize: this.maxFileSize(),
                fileEditable: this.fileEditable(),
            },
        }), ...(ngDevMode ? [{ debugName: "node" }] : []));
    }
    onContextChanged(event) {
        this.context.set(event.data);
    }
    //#endregion
    //#region ----   Lifecycle Methods   ----
    async ngOnInit() {
        if (Array.isArray(this.files()) && typeof this.files()[0] === 'string') {
            this.isLoading = true;
            // Convert string file IDs to AXPFileListItem objects using batch API
            const prefixedIds = this.files();
            const actualIds = prefixedIds.map((id) => this.removeFilePrefix(id));
            let fileItems = [];
            try {
                const infos = await this.fileService.findMany(actualIds);
                const infoMap = new Map(infos.map((i) => [i.fileId, i]));
                fileItems = prefixedIds.map((prefixedId) => {
                    const actualId = this.removeFilePrefix(prefixedId);
                    const info = infoMap.get(actualId);
                    if (info) {
                        return {
                            id: info.fileId,
                            name: info.name || '',
                            size: info.size,
                            status: 'uploaded',
                            source: {
                                kind: 'fileId',
                                value: info.fileId,
                            },
                        };
                    }
                    return {
                        id: actualId,
                        name: 'Unknown File',
                        size: 0,
                        status: 'deleted',
                        source: { kind: 'none', value: '' },
                    };
                });
            }
            catch (error) {
                console.error('Failed to get file infos', error);
                fileItems = prefixedIds.map((prefixedId) => ({
                    id: this.removeFilePrefix(prefixedId),
                    name: 'Unknown File',
                    size: 0,
                    status: 'deleted',
                    source: { kind: 'none', value: '' },
                }));
            }
            this.isLoading = false;
            // Update the context with the converted file items
            this.context.set({ files: fileItems });
        }
    }
    removeFilePrefix(fileId) {
        return fileId.startsWith('file:') ? fileId.substring(5) : fileId;
    }
    //#endregion
    //#region ----   UI Handlers   ----
    apply() {
        this.close({
            data: this.context(),
        });
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: AXPFileListPopupComponent, deps: null, target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "17.0.0", version: "20.3.16", type: AXPFileListPopupComponent, isStandalone: true, selector: "ng-component", inputs: { files: { classPropertyName: "files", publicName: "files", isSignal: true, isRequired: false, transformFunction: null }, readOnly: { classPropertyName: "readOnly", publicName: "readOnly", isSignal: true, isRequired: false, transformFunction: null }, multiple: { classPropertyName: "multiple", publicName: "multiple", isSignal: true, isRequired: false, transformFunction: null }, fileEditable: { classPropertyName: "fileEditable", publicName: "fileEditable", isSignal: true, isRequired: false, transformFunction: null }, accept: { classPropertyName: "accept", publicName: "accept", isSignal: true, isRequired: false, transformFunction: null }, maxFileSize: { classPropertyName: "maxFileSize", publicName: "maxFileSize", isSignal: true, isRequired: false, transformFunction: null } }, usesInheritance: true, ngImport: i0, template: "<div class=\"ax-p-2\">\n  <axp-widgets-container [context]=\"context()\" (onContextChanged)=\"onContextChanged($event)\">\n    <ng-container axp-widget-renderer [node]=\"node()\" [mode]=\"mode()\"></ng-container>\n  </axp-widgets-container>\n</div>\n<ax-footer>\n  <ax-suffix>\n    @if (readOnly()) {\n      <ax-button [text]=\"'@general:actions.close.title' | translate | async\" (click)=\"close()\"></ax-button>\n    } @else {\n      <ax-button [text]=\"'@general:actions.discard.title' | translate | async\" (click)=\"close()\"></ax-button>\n      <ax-button\n        [text]=\"'@general:actions.apply.title' | translate | async\"\n        (click)=\"apply()\"\n        [color]=\"'primary'\"\n      ></ax-button>\n    }\n  </ax-suffix>\n</ax-footer>\n", dependencies: [{ kind: "ngmodule", type: CommonModule }, { kind: "ngmodule", type: 
                //
                AXLabelModule }, { kind: "ngmodule", type: AXCheckBoxModule }, { kind: "ngmodule", type: AXDecoratorModule }, { kind: "component", type: i2.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: i1.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: "ngmodule", type: 
                //
                AXPWidgetCoreModule }, { kind: "component", type: i1$1.AXPWidgetContainerComponent, selector: "axp-widgets-container", inputs: ["context", "functions"], outputs: ["onContextChanged"] }, { kind: "directive", type: i1$1.AXPWidgetRendererDirective, selector: "[axp-widget-renderer]", inputs: ["parentNode", "index", "mode", "node"], outputs: ["onOptionsChanged", "onValueChanged", "onLoad"], exportAs: ["widgetRenderer"] }, { kind: "pipe", type: i1$2.AsyncPipe, name: "async" }, { kind: "pipe", type: i5.AXTranslatorPipe, name: "translate" }] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.16", ngImport: i0, type: AXPFileListPopupComponent, decorators: [{
            type: Component,
            args: [{ imports: [
                        CommonModule,
                        //
                        AXLabelModule,
                        AXCheckBoxModule,
                        AXDecoratorModule,
                        AXButtonModule,
                        AXTranslationModule,
                        //
                        AXPWidgetCoreModule
                    ], template: "<div class=\"ax-p-2\">\n  <axp-widgets-container [context]=\"context()\" (onContextChanged)=\"onContextChanged($event)\">\n    <ng-container axp-widget-renderer [node]=\"node()\" [mode]=\"mode()\"></ng-container>\n  </axp-widgets-container>\n</div>\n<ax-footer>\n  <ax-suffix>\n    @if (readOnly()) {\n      <ax-button [text]=\"'@general:actions.close.title' | translate | async\" (click)=\"close()\"></ax-button>\n    } @else {\n      <ax-button [text]=\"'@general:actions.discard.title' | translate | async\" (click)=\"close()\"></ax-button>\n      <ax-button\n        [text]=\"'@general:actions.apply.title' | translate | async\"\n        (click)=\"apply()\"\n        [color]=\"'primary'\"\n      ></ax-button>\n    }\n  </ax-suffix>\n</ax-footer>\n" }]
        }], propDecorators: { files: [{ type: i0.Input, args: [{ isSignal: true, alias: "files", required: false }] }], readOnly: [{ type: i0.Input, args: [{ isSignal: true, alias: "readOnly", required: false }] }], multiple: [{ type: i0.Input, args: [{ isSignal: true, alias: "multiple", required: false }] }], fileEditable: [{ type: i0.Input, args: [{ isSignal: true, alias: "fileEditable", required: false }] }], accept: [{ type: i0.Input, args: [{ isSignal: true, alias: "accept", required: false }] }], maxFileSize: [{ type: i0.Input, args: [{ isSignal: true, alias: "maxFileSize", required: false }] }] } });

export { AXPFileListPopupComponent };
//# sourceMappingURL=acorex-platform-layout-widgets-file-list-popup.component-BfV3spe3.mjs.map
