import { Model } from '../core/Model.js';
import { exponentialBackoffDelay, shouldRetryApiError, retry } from './retry_logic.js';
import { fromError } from '../exceptions/builder.js';
import { LiveData } from '../reactive/LiveData.js';
export var ModelState;
(function (ModelState) {
    /**
     * Not doing anything.
     */
    ModelState[ModelState["idle"] = 0] = "idle";
    /**
     * Currently querying for breadcrumb data.
     */
    ModelState[ModelState["reading"] = 1] = "reading";
})(ModelState || (ModelState = {}));
export default class BreadcrumbsModel extends Model {
    /**
     * The current state of the model.
     */
    state = ModelState.idle;
    /**
     * The current breadcrumbs
     */
    breadcrumbs = new LiveData([]);
    /**
     * A reference to the SDK service used to perform API calls.
     */
    sdk;
    /**
     * The current Organization ID.
     */
    oid;
    constructor(sdk, oid) {
        super();
        this.sdk = sdk;
        this.oid = oid;
    }
    reset() {
        this.state = ModelState.idle;
        this.breadcrumbs.set([]);
    }
    async scheduleQuery(parent) {
        this.debounce(() => this.#query(parent), parent);
        await this.taskComplete;
    }
    async #query(parent) {
        if (this.state !== ModelState.idle) {
            // This should not happen due to debouncing, but just in case
            return;
        }
        if (!parent) {
            this.breadcrumbs.set([]);
            return;
        }
        this.state = ModelState.reading;
        const operation = async () => {
            return this.sdk.file.breadcrumbs(this.oid, parent);
        };
        const retryOptions = {
            retries: this.retryCount,
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
        };
        try {
            const br = await retry(operation, retryOptions);
            this.breadcrumbs.set(br.items);
        }
        catch (e) {
            const err = fromError(e);
            this.notifyError(err);
        }
        finally {
            this.state = ModelState.idle;
        }
    }
}
//# sourceMappingURL=BreadcrumbsModel.js.map