"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    customEvents: function() {
        return customEvents;
    },
    useCustomEvent: function() {
        return useCustomEvent;
    },
    useCustomEventsPath: function() {
        return useCustomEventsPath;
    },
    useParams: function() {
        return useParams;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _react = require("react");
var _reactfastcompare = /*#__PURE__*/ _interop_require_default._(require("react-fast-compare"));
var _taro = require("@tarojs/taro");
var _useforceupdate = require("./use-force-update");
var customEvents = new _taro.Events();
function useCustomEventsPath(selector) {
    var _getCurrentInstance_router;
    selector = selector || '';
    var path = (_getCurrentInstance_router = (0, _taro.getCurrentInstance)().router) === null || _getCurrentInstance_router === void 0 ? void 0 : _getCurrentInstance_router.path;
    return path ? "".concat(path, "__").concat(selector) : selector;
}
function useCustomEvent(selector, cb) {
    var path = useCustomEventsPath(selector);
    (0, _react.useEffect)(function() {
        customEvents.on(path, cb);
        return function() {
            customEvents.off(path);
        };
    }, []);
    var trigger = function trigger(args) {
        customEvents.trigger(path, args);
    };
    var off = function off() {
        customEvents.off(path);
    };
    return [
        trigger,
        off
    ];
}
function useParams(args) {
    var forceUpdate = (0, _useforceupdate.useForceUpdate)();
    var stateRef = (0, _react.useRef)(args);
    var currentRef = (0, _react.useRef)();
    var previousRef = (0, _react.useRef)();
    if (!(0, _reactfastcompare.default)(currentRef.current, args)) {
        previousRef.current = currentRef.current;
        currentRef.current = args;
        stateRef.current = args;
    }
    var setParams = function setParams(args) {
        stateRef.current = (0, _object_spread._)({}, stateRef.current, args);
        forceUpdate();
    };
    var params = stateRef.current;
    return {
        params: params,
        setParams: setParams
    };
}
