import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
import { createSelectorQuery } from "@tarojs/taro";
import { MiniLru } from "../lru";
import { getRect, inBrowser } from "../get-rect";
var lru = new MiniLru(10);
var createScopedSelectorQuery = function(scope) {
    var query = createSelectorQuery();
    return scope ? query.in(scope) : query;
};
export function makeRect(width, height) {
    return {
        top: 0,
        left: 0,
        right: width,
        bottom: height,
        width: width,
        height: height
    };
}
export var getRectInMultiPlatform = function(element) {
    var harmonyId = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : '';
    return _async_to_generator(function() {
        return _ts_generator(this, function(_state) {
            if (element) {
                if (inBrowser) {
                    return [
                        2,
                        Promise.resolve(getRect(element))
                    ];
                }
                // 小程序下的逻辑
                return [
                    2,
                    new Promise(function(resolve, reject) {
                        if (lru.has(element)) {
                            resolve(lru.get(element));
                            return;
                        }
                        createScopedSelectorQuery(element === null || element === void 0 ? void 0 : element._scope)// https://taro.jd.com/docs/reference/config/page/
                        .select("#".concat(harmonyId || element.uid)).boundingClientRect().exec(function(param) {
                            var _param = _sliced_to_array(param, 1), rects = _param[0];
                            if (rects) {
                                lru.set(element, rects);
                            }
                            resolve(rects);
                        });
                    })
                ];
            }
            return [
                2,
                Promise.resolve(makeRect(0, 0))
            ];
        });
    })();
};
// 获取元素位置，不走缓存
export var getRectInMultiPlatformWithoutCache = function(element) {
    var harmonyId = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : '';
    return _async_to_generator(function() {
        return _ts_generator(this, function(_state) {
            if (element) {
                if (inBrowser) {
                    return [
                        2,
                        Promise.resolve(getRect(element))
                    ];
                }
                return [
                    2,
                    new Promise(function(resolve, reject) {
                        createScopedSelectorQuery(element === null || element === void 0 ? void 0 : element._scope)// https://taro.jd.com/docs/reference/config/page/
                        .select("#".concat(harmonyId || element.uid)).boundingClientRect().exec(function(param) {
                            var _param = _sliced_to_array(param, 1), rects = _param[0];
                            resolve(rects);
                        });
                    })
                ];
            }
            return [
                2,
                Promise.resolve(makeRect(0, 0))
            ];
        });
    })();
};
