import { UMB_DOCUMENT_TYPE_ENTITY_TYPE } from '../../entity.js';
import { DocumentTypeService } from '../../../../core/backend-api/index.js';
import { UmbContentTypeStructureServerDataSourceBase } from '../../../../content/content-type/index.js';
import { tryExecute } from '../../../../core/resources/index.js';
/**
 *
 
 * @class UmbDocumentTypeStructureServerDataSource
 * @augments {UmbContentTypeStructureServerDataSourceBase}
 */
export class UmbDocumentTypeStructureServerDataSource extends UmbContentTypeStructureServerDataSourceBase {
    #host;
    constructor(host) {
        super(host, { getAllowedChildrenOf, mapper });
        this.#host = host;
    }
    async getAllowedParentsOf(unique) {
        const { data, error } = await tryExecute(this.#host, DocumentTypeService.getDocumentTypeByIdAllowedParents({
            path: { id: unique },
        }));
        if (error) {
            return { error };
        }
        const mappedData = data?.allowedParentIds.map((item) => ({
            unique: item.id,
            entityType: UMB_DOCUMENT_TYPE_ENTITY_TYPE,
        })) ?? [];
        return { data: mappedData };
    }
}
const getAllowedChildrenOf = (unique, parentContentUnique) => {
    if (unique) {
        // eslint-disable-next-line local-rules/no-direct-api-import
        return DocumentTypeService.getDocumentTypeByIdAllowedChildren({
            path: { id: unique },
            query: { parentContentKey: parentContentUnique ?? undefined },
        });
    }
    else {
        // eslint-disable-next-line local-rules/no-direct-api-import
        return DocumentTypeService.getDocumentTypeAllowedAtRoot({});
    }
};
const mapper = (item) => {
    return {
        unique: item.id,
        entityType: UMB_DOCUMENT_TYPE_ENTITY_TYPE,
        name: item.name,
        description: item.description || null,
        icon: item.icon || null,
    };
};
