import React from 'react';
export function useSet(_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.initialValue, initialValue = _c === void 0 ? new Set() : _c, onChange = _b.onChange, onAdd = _b.onAdd, onRemove = _b.onRemove;
    var _d = React.useState(initialValue), collection = _d[0], setCollection = _d[1];
    var removeFromSet = React.useCallback(function (collection, item) {
        collection.delete(item);
        onRemove && onRemove(item);
        return new Set(collection);
    }, [onRemove]);
    var addToSet = React.useCallback(function (collection, item) {
        collection.add(item);
        onAdd && onAdd(item);
        return new Set(collection);
    }, [onAdd]);
    var updateCollection = React.useCallback(function (updater) {
        setCollection(function (prevCollection) {
            var newCollection = updater(prevCollection);
            onChange && onChange(newCollection);
            return newCollection;
        });
    }, [onChange]);
    var has = React.useCallback(function (item) { return collection.has(item); }, [collection]);
    var add = React.useCallback(function (item) {
        return updateCollection(function (prevCollection) { return addToSet(prevCollection, item); });
    }, [addToSet, updateCollection]);
    var remove = React.useCallback(function (item) {
        return updateCollection(function (prevCollection) { return removeFromSet(prevCollection, item); });
    }, [removeFromSet, updateCollection]);
    var set = React.useCallback(function (newCollection) { return updateCollection(function () { return newCollection; }); }, [updateCollection]);
    var toggle = React.useCallback(function (item) {
        return updateCollection(function (prevCollection) {
            return prevCollection.has(item)
                ? removeFromSet(prevCollection, item)
                : addToSet(prevCollection, item);
        });
    }, [addToSet, removeFromSet, updateCollection]);
    var removeAll = React.useCallback(function () { return updateCollection(function () { return new Set(); }); }, [updateCollection]);
    return {
        collection: collection,
        setCollection: set,
        has: has,
        add: add,
        remove: remove,
        removeAll: removeAll,
        toggle: toggle,
    };
}
//# sourceMappingURL=useSet.js.map