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 file 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 FileModel extends Model {
    /**
     * The current state of the model.
     */
    state = ModelState.idle;
    /**
     * 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;
    }
    async read(key) {
        this.debounce(this.#read.bind(this, key), key);
        await this.taskComplete;
        return this.value;
    }
    async #read(key) {
        if (this.state !== ModelState.idle) {
            return;
        }
        this.state = ModelState.reading;
        const retryOptions = {
            delayMs: exponentialBackoffDelay,
            shouldRetry: shouldRetryApiError,
            retries: this.retryCount,
        };
        const operation = async () => {
            return this.sdk.file.read(this.oid, key);
        };
        try {
            const result = await retry(operation, retryOptions);
            this.raw = new LiveData(result);
        }
        catch (e) {
            const err = fromError(e);
            this.notifyError(err);
            throw err;
        }
        finally {
            this.state = ModelState.idle;
        }
        this.dispatchEvent(new Event('querycomplete'));
    }
}
//# sourceMappingURL=FileModel.js.map