"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Rendering
 */
var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) {
    if (value !== null && value !== void 0) {
        if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected.");
        var dispose, inner;
        if (async) {
            if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined.");
            dispose = value[Symbol.asyncDispose];
        }
        if (dispose === void 0) {
            if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined.");
            dispose = value[Symbol.dispose];
            if (async) inner = dispose;
        }
        if (typeof dispose !== "function") throw new TypeError("Object not disposable.");
        if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } };
        env.stack.push({ value: value, dispose: dispose, async: async });
    }
    else if (async) {
        env.stack.push({ async: true });
    }
    return value;
};
var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) {
    return function (env) {
        function fail(e) {
            env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e;
            env.hasError = true;
        }
        var r, s = 0;
        function next() {
            while (r = env.stack.pop()) {
                try {
                    if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next);
                    if (r.dispose) {
                        var result = r.dispose.call(r.value);
                        if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); });
                    }
                    else s |= 1;
                }
                catch (e) {
                    fail(e);
                }
            }
            if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve();
            if (env.hasError) throw env.error;
        }
        return next();
    };
})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
    var e = new Error(message);
    return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
});
Object.defineProperty(exports, "__esModule", { value: true });
exports.queryVisibleFeatures = queryVisibleFeatures;
const core_bentley_1 = require("@itwin/core-bentley");
const Pixel_1 = require("../../render/Pixel");
/** Ensures that the iterable supplied to QueryVisibleFeaturesCallback becomes invalidated once the callback exits.
 * The iterable relies on RenderTarget state that changes from one frame to another.
 */
class ExpiringIterable {
    _features;
    _disposed = false;
    constructor(features) {
        this._features = features;
    }
    [Symbol.dispose]() {
        this._disposed = true;
        this._features = [];
    }
    [Symbol.iterator]() {
        (0, core_bentley_1.assert)(!this._disposed, "The iterable supplied to QueryVisibleFeaturesCallback is valid only for the duration of the callback.");
        return this._features[Symbol.iterator]();
    }
}
function invokeCallback(features, callback) {
    const env_1 = { stack: [], error: void 0, hasError: false };
    try {
        const iterable = __addDisposableResource(env_1, new ExpiringIterable(features), false);
        callback(iterable);
    }
    catch (e_1) {
        env_1.error = e_1;
        env_1.hasError = true;
    }
    finally {
        __disposeResources(env_1);
    }
}
/** Features read from pixels rendered by a viewport. */
class ScreenFeatures {
    _pixels;
    _rect;
    _iModel;
    constructor(pixels, rect, viewport) {
        this._pixels = pixels;
        this._rect = rect.clone();
        this._rect.right = viewport.cssPixelsToDevicePixels(this._rect.right);
        this._rect.bottom = viewport.cssPixelsToDevicePixels(this._rect.bottom);
        this._iModel = viewport.iModel;
    }
    [Symbol.iterator]() {
        function* iterator(pixels, rect, iModel) {
            for (let x = rect.left; x < rect.right; x++) {
                for (let y = rect.top; y < rect.bottom; y++) {
                    const pixel = pixels.getPixel(x, y);
                    if (pixel.feature && pixel.modelId) {
                        yield {
                            elementId: pixel.feature.elementId,
                            subCategoryId: pixel.feature.subCategoryId,
                            geometryClass: pixel.feature.geometryClass,
                            modelId: pixel.modelId,
                            iModel: pixel.iModel ?? iModel,
                        };
                    }
                }
            }
        }
        return iterator(this._pixels, this._rect, this._iModel);
    }
}
/** Implementation of [[Viewport.queryVisibleFeatures]]. */
function queryVisibleFeatures(viewport, options, callback) {
    (0, core_bentley_1.assert)("screen" === options.source || "tiles" === options.source);
    switch (options.source) {
        case "screen":
            const rect = options.rect ?? viewport.viewRect;
            viewport.readPixels(rect, Pixel_1.Pixel.Selector.Feature, (pixels) => invokeCallback(pixels ? new ScreenFeatures(pixels, rect, viewport) : [], callback), true !== options.includeNonLocatable);
            break;
        case "tiles":
            viewport.target.queryVisibleTileFeatures(options, viewport.iModel, (features) => invokeCallback(features, callback));
            break;
        default:
            invokeCallback([], callback);
            break;
    }
}
//# sourceMappingURL=QueryVisibileFeatures.js.map