import _set from "lodash/set";
import { useMemo } from "react";
import { useHandler } from "../../../hooks/useHandler";
export default (function (props) {
  var updateSettings = useHandler(props, function (_ref) {
    var element = _ref.element,
        updateElement = _ref.updateElement,
        dataNamespace = _ref.dataNamespace;
    var historyUpdated = {};
    return function (name, newValue) {
      var history = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
      var propName = "".concat(dataNamespace, ".").concat(name);

      var newElement = _set(element, propName, newValue);

      if (!history) {
        updateElement({
          element: newElement,
          history: history,
          merge: true
        });
        return;
      }

      if (historyUpdated[propName] !== newValue) {
        historyUpdated[propName] = newValue;
        updateElement({
          element: newElement,
          merge: true
        });
      }
    };
  });
  var getUpdateValue = useMemo(function () {
    var handlers = {};
    return function (name) {
      if (!handlers[name]) {
        handlers[name] = function (value) {
          return updateSettings(name, value, true);
        };
      }

      return handlers[name];
    };
  }, [updateSettings]);
  var getUpdatePreview = useMemo(function () {
    var handlers = {};
    return function (name) {
      if (!handlers[name]) {
        handlers[name] = function (value) {
          return updateSettings(name, value, false);
        };
      }

      return handlers[name];
    };
  }, [updateSettings]);
  return {
    getUpdateValue: getUpdateValue,
    getUpdatePreview: getUpdatePreview
  };
});
//# sourceMappingURL=useUpdateHandlers.js.map