import { UmbBooleanState, UmbObjectState } from '../../../../../libs/observable-api/index.js';
import { UmbControllerBase } from '../../../../../libs/class-api/index.js';
import { UmbDataTypeDetailRepository } from '../../../../data-type/index.js';
import { UmbPropertyEditorConfigCollection } from '../../../../core/property-editor/index.js';
export class UmbContentCollectionManager extends UmbControllerBase {
    #host;
    #collectionAlias;
    #collectionConfig;
    #manifestOverrides;
    #hasCollection;
    #dataTypeDetailRepository;
    constructor(host, structureManager, collectionAlias) {
        super(host);
        this.#collectionConfig = new UmbObjectState(undefined);
        this.collectionConfig = this.#collectionConfig.asObservable();
        this.#manifestOverrides = new UmbObjectState(undefined);
        this.manifestOverrides = this.#manifestOverrides.asObservable();
        this.#hasCollection = new UmbBooleanState(false);
        this.hasCollection = this.#hasCollection.asObservable();
        this.#dataTypeDetailRepository = new UmbDataTypeDetailRepository(this);
        this.#host = host;
        this.#collectionAlias = collectionAlias;
        this.observe(collectionAlias ? structureManager.ownerContentType : undefined, async (contentType) => {
            this.#hasCollection.setValue(!!contentType?.collection);
            const dataTypeUnique = contentType?.collection?.unique;
            if (dataTypeUnique) {
                this.#dataTypeDetailRepository.requestByUnique(dataTypeUnique);
                this.observe(await this.#dataTypeDetailRepository.byUnique(dataTypeUnique), (dataType) => {
                    this.#gotDataType(dataType);
                }, '_observeConfigDataType');
            }
        }, null);
    }
    getCollectionAlias() {
        return this.#collectionAlias;
    }
    #gotDataType(dataType) {
        if (!dataType) {
            this.#collectionConfig.setValue(undefined);
            this.#manifestOverrides.setValue(undefined);
            this.removeUmbControllerByAlias('_observeHostUnique');
            return;
        }
        const config = new UmbPropertyEditorConfigCollection(dataType.values);
        const pageSize = Number(config.getValueByAlias('pageSize'));
        this.#collectionConfig.setValue({
            unique: this.#host.getUnique(),
            layouts: config?.getValueByAlias('layouts'),
            orderBy: config?.getValueByAlias('orderBy') ?? 'updateDate',
            orderDirection: config?.getValueByAlias('orderDirection') ?? 'asc',
            pageSize: isNaN(pageSize) ? 50 : pageSize,
            userDefinedProperties: config?.getValueByAlias('includeProperties'),
        });
        // Observe the workspace unique so the collection config stays in sync
        // when unique is set after initial config creation (e.g. during createScaffold).
        // Only observing when a config is set.
        this.observe(this.#host.unique, (unique) => {
            this.#collectionConfig.update({ unique });
        }, '_observeHostUnique');
        const overrides = {
            alias: 'Umb.WorkspaceView.Content.Collection',
            meta: {},
        };
        const overrideIcon = config?.getValueByAlias('icon');
        if (overrideIcon && overrideIcon !== '') {
            overrides.meta.icon = overrideIcon;
        }
        const overrideLabel = config?.getValueByAlias('tabName');
        if (overrideLabel && overrideLabel !== '') {
            overrides.meta.label = overrideLabel;
        }
        const showFirst = config?.getValueByAlias('showContentFirst');
        if (showFirst === true) {
            overrides.weight = 150;
        }
        this.#manifestOverrides.setValue(overrides);
    }
}
