"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useComponentProps = exports.useScrollTop = void 0;
var tslib_1 = require("tslib");
var React = tslib_1.__importStar(require("react"));
var react_router_dom_1 = require("react-router-dom");
var adapter_1 = require("./adapter");
var global_api_1 = require("../app/global-api");
var widgets_1 = require("./widgets");
function useScrollTop() {
    var pathname = (0, react_router_dom_1.useLocation)().pathname;
    React.useEffect(function () {
        adapter_1.runtime === null || adapter_1.runtime === void 0 ? void 0 : adapter_1.runtime.scrollTo(0, 0);
    }, [pathname]);
    return null;
}
exports.useScrollTop = useScrollTop;
/**
 * 用于处理自定义组件props传参结构，对系统变量进行保留
 * 曾用名：resolveComponentProps()
 */
function useComponentProps(props, isPlainProps) {
    var _a;
    var emitRef = React.useRef(props.emit);
    var _b = (global_api_1.app || {}).__internal__, __internal__ = _b === void 0 ? {} : _b;
    var resolveStaticResourceUrl = (__internal__ || {}).resolveStaticResourceUrl;
    var reactBuiltinPorps = React.useMemo(function () { return ['ref', 'key', 'dangerouslySetInnerHTML', 'className', 'htmlFor', 'style', 'contentEditable', 'children']; }, []);
    var builtinProps = React.useMemo(function () { return tslib_1.__spreadArray(tslib_1.__spreadArray(tslib_1.__spreadArray([], tslib_1.__read(reactBuiltinPorps), false), tslib_1.__read(widgets_1.widgetKeys), false), [
        'events',
        'classList',
        'widgetType',
        'staticResourceAttribute',
        // 小程序保留字
        'id',
        'class',
        'hidden',
        'slot',
    ], false); }, []);
    var _c = props.data, data = _c === void 0 ? {} : _c, staticResourceAttribute = props.staticResourceAttribute, emit = props.emit, events = props.events, 
    /**
     * 排除复合组件所需独有属性
     */
    _ = props.compositeParent, __ = props.forIndexes, restProps = tslib_1.__rest(props, ["data", "staticResourceAttribute", "emit", "events", "compositeParent", "forIndexes"]);
    (_a = staticResourceAttribute === null || staticResourceAttribute === void 0 ? void 0 : staticResourceAttribute.map) === null || _a === void 0 ? void 0 : _a.call(staticResourceAttribute, function (property) {
        var _a;
        if ((_a = props.data) === null || _a === void 0 ? void 0 : _a[property]) {
            props.data[property] = resolveStaticResourceUrl(props.data[property]);
        }
    });
    if (!isPlainProps) {
        return props;
    }
    var customProps = tslib_1.__assign({}, data);
    // delete builtin props
    builtinProps.map(function (prop) { return delete customProps[prop]; });
    // 选区events处理
    var _selectableBlockEvents = customProps._selectableBlockEvents, restData = tslib_1.__rest(customProps, ["_selectableBlockEvents"]);
    var _selectableBlockEmit = _selectableBlockEvents.emit;
    _selectableBlockEvents.events = new Proxy({}, {
        get: function (obj, prop) {
            return prop in obj ? obj[prop] : function (e) { return _selectableBlockEmit(prop, e); };
        },
    });
    emitRef.current = emit;
    /**
     * TODO: 可不依赖配置的事件列表，使用通用 proxy
     * 但存在问题：vue.CreateElement 中 on 可能遍历了 events 对象
     * proxy 会导致获取不到事件
     */
    var proxyEvents = React.useMemo(function () {
        return new Proxy(events.reduce(function (events, item) {
            var propName = item;
            events[propName] = function (e) { return emitRef.current(propName, e); };
            return events;
        }, {}), {
            get: function (obj, prop) {
                return prop in obj ? obj[prop] : function (e) { return emitRef.current(prop, e); };
            },
        });
    }, [emitRef, events]);
    return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({}, restData), restProps), { _selectableBlockEvents: _selectableBlockEvents, 
        /**
         * 覆盖原有的 events 事件名数组，采用通用代理
         */
        events: proxyEvents, emit: emit });
}
exports.useComponentProps = useComponentProps;
//# sourceMappingURL=hooks.js.map