import { buildCustomEvent } from "../../../../utils/event-utils.js";
const foldedItemListContextEventName = 'atomic/resolveFoldedResultList';
class MissingParentError extends Error {
    constructor(elementName){
        super(`The "${elementName}" element must be the child of an "atomic-folded-result-list" or "atomic-insight-folded-result-list" element.`);
    }
}
class FoldedItemListContextController {
    get foldedItemList() {
        return this._error ? null : this._foldedItemList;
    }
    get error() {
        return this._error;
    }
    get hasError() {
        return null !== this._error;
    }
    hostConnected() {
        this._resolveFoldedItemListContext();
    }
    _resolveFoldedItemListContext() {
        const event = buildCustomEvent(foldedItemListContextEventName, (foldedItemList)=>{
            this._foldedItemList = foldedItemList;
            this.host.requestUpdate();
        });
        const canceled = this.host.dispatchEvent(event);
        if (canceled) {
            const elementName = this.host.nodeName.toLowerCase();
            this._error = new MissingParentError(elementName);
            this._foldedItemList = null;
            this.host.error = this._error;
            this.host.requestUpdate();
        }
    }
    constructor(host){
        this._foldedItemList = null;
        this._error = null;
        this.host = host;
        host.addController(this);
    }
}
export { FoldedItemListContextController, MissingParentError };
