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';
var ModelState;
(function (ModelState) {
    /**
     * Not doing anything.
     */
    ModelState[ModelState["idle"] = 0] = "idle";
    /**
     * Currently querying for breadcrumb data.
     */
    ModelState[ModelState["reading"] = 1] = "reading";
    /**
     * Data was received and the result yielded no results or less than the limit.
     */
    ModelState[ModelState["ended"] = 2] = "ended";
})(ModelState || (ModelState = {}));
export default class TrashModel extends Model {
    oid;
    sdk;
    /**
     * The current state of the model.
     */
    state = ModelState.idle;
    /**
     * The trash items
     */
    items = new LiveData([]);
    /**
     * The pagination cursor for files to use with the next query.
     */
    cursor;
    constructor(oid, sdk) {
        super();
        this.oid = oid;
        this.sdk = sdk;
    }
    reset() {
        this.state = ModelState.idle;
        this.cursor = undefined;
        this.items.set([]);
    }
    scheduleQuery() {
        this.debounce(this.#query);
    }
    async #query() {
        if (this.state !== ModelState.idle) {
            return;
        }
        this.state = ModelState.reading;
        const operation = async () => {
            return this.sdk.trash.list(this.oid, this.getPageQueryOptions());
        };
        const retryOptions = {
            retries: this.retryCount,
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
        };
        try {
            const result = await retry(operation, retryOptions);
            this.#processTrashResults(result);
        }
        catch (e) {
            const err = fromError(e);
            this.notifyError(err);
            this.state = ModelState.idle;
        }
    }
    #processTrashResults(result) {
        const { items, cursor } = result;
        if (cursor) {
            this.cursor = cursor;
        }
        if (!items.length) {
            this.state = ModelState.ended;
            return;
        }
        this.items.set(this.items.value.concat(items));
        this.state = ModelState.idle;
    }
    getPageQueryOptions() {
        const { cursor, limit } = this;
        const opts = {};
        if (cursor) {
            opts.cursor = cursor;
        }
        if (typeof limit === 'number' && limit) {
            opts.limit = limit;
        }
        return opts;
    }
    /**
     * Finds a file by the key.
     * @param key The key of the file to find
     * @returns The file, if any
     */
    getItem(key) {
        return this.items.value.find((i) => i.key === key);
    }
    delete(keys) {
        return this.sdk.trash.delete(this.oid, keys);
    }
    restore(keys) {
        return this.sdk.trash.restore(this.oid, keys);
    }
    removeItems(keys) {
        this.items.set(this.items.value.filter((item) => !keys.includes(item.key)));
    }
}
//# sourceMappingURL=TrashModel.js.map