"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = useControlledState;
const react_1 = require("react");
const use_layout_effect_1 = __importDefault(require("./use-layout-effect"));
/**
 * Similar to `useState` but will use props value if provided.
 * From React 18, we do not need safe `useState` since it will not throw for unmounted update.
 * This hooks remove the `onChange` & `postState` logic since we only need basic merged state logic.
 */
function useControlledState(defaultStateValue, value) {
    const [innerValue, setInnerValue] = (0, react_1.useState)(defaultStateValue);
    const mergedValue = value !== undefined ? value : innerValue;
    (0, use_layout_effect_1.default)((mount) => {
        if (!mount) {
            setInnerValue(value);
        }
    }, [value]);
    return [
        // Value
        mergedValue,
        // Update function
        setInnerValue,
    ];
}
