import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { useEffect, useRef } from "react";
import isEqual from "react-fast-compare";
import { Events, getCurrentInstance } from "@tarojs/taro";
import { useForceUpdate } from "./use-force-update";
export var customEvents = new Events();
export function useCustomEventsPath(selector) {
    var _getCurrentInstance_router;
    selector = selector || '';
    var path = (_getCurrentInstance_router = getCurrentInstance().router) === null || _getCurrentInstance_router === void 0 ? void 0 : _getCurrentInstance_router.path;
    return path ? "".concat(path, "__").concat(selector) : selector;
}
export function useCustomEvent(selector, cb) {
    var path = useCustomEventsPath(selector);
    useEffect(function() {
        customEvents.on(path, cb);
        return function() {
            customEvents.off(path);
        };
    }, []);
    var trigger = function(args) {
        customEvents.trigger(path, args);
    };
    var off = function() {
        customEvents.off(path);
    };
    return [
        trigger,
        off
    ];
}
export function useParams(args) {
    var forceUpdate = useForceUpdate();
    var stateRef = useRef(args);
    var currentRef = useRef();
    var previousRef = useRef();
    if (!isEqual(currentRef.current, args)) {
        previousRef.current = currentRef.current;
        currentRef.current = args;
        stateRef.current = args;
    }
    var setParams = function(args) {
        stateRef.current = _object_spread({}, stateRef.current, args);
        forceUpdate();
    };
    var params = stateRef.current;
    return {
        params: params,
        setParams: setParams
    };
}
