import { useGetEntityListSubTree, useAccessorTreeState, AccessorTree, Entity } from "@contember/binding";
import { useDialog, Stack, Button } from "@contember/ui";
import { useMemo } from "react";
import { choiceFieldDictionary } from "../choiceFieldDictionary.js";
import { useDesugaredOptionPath } from "./useDesugaredOptionPath.js";
import { jsxDEV } from "react/jsx-dev-runtime";
import { useMessageFormatter } from "../../../../../i18n/useMessageFormatter.js";
var _jsxFileName = "/__w/interface/interface/packages/admin/src/components/bindingFacade/fields/ChoiceField/hooks/useOnAddNew.tsx";
const useOnAddNew = ({
  createNewForm,
  connect,
  ...props
}) => {
  const desugaredOptionPath = useDesugaredOptionPath(props, void 0);
  const getSubTree = useGetEntityListSubTree();
  const dialog = useDialog();
  const localization = useMessageFormatter(choiceFieldDictionary);
  const accessorTreeState = useAccessorTreeState();
  return useMemo(() => {
    if (!createNewForm) {
      return void 0;
    }
    return async () => {
      const subTree = getSubTree({
        entities: {
          entityName: desugaredOptionPath.entityName
        },
        limit: 0
      });
      const newEntityId = subTree.createNewEntity();
      const entity = subTree.getChildEntityById(newEntityId.value);
      const result = await dialog.openDialog({
        heading: localization("choiceField.createNew.dialogTitle"),
        content: (contentProps) => /* @__PURE__ */ jsxDEV(Stack, {
          direction: "vertical",
          children: [/* @__PURE__ */ jsxDEV(AccessorTree, {
            state: accessorTreeState,
            children: /* @__PURE__ */ jsxDEV(Entity, {
              accessor: entity,
              children: createNewForm
            }, void 0, false, {
              fileName: _jsxFileName,
              lineNumber: 41,
              columnNumber: 9
            }, void 0)
          }, void 0, false, {
            fileName: _jsxFileName,
            lineNumber: 40,
            columnNumber: 8
          }, void 0), /* @__PURE__ */ jsxDEV(Stack, {
            direction: "horizontal",
            evenly: true,
            children: [/* @__PURE__ */ jsxDEV(Button, {
              onClick: () => contentProps.resolve(),
              distinction: "default",
              elevation: "none",
              children: localization("choiceField.createNew.cancelButtonText")
            }, void 0, false, {
              fileName: _jsxFileName,
              lineNumber: 44,
              columnNumber: 9
            }, void 0), /* @__PURE__ */ jsxDEV(Button, {
              onClick: () => contentProps.resolve(true),
              distinction: "primary",
              elevation: "none",
              children: localization("choiceField.createNew.confirmButtonText")
            }, void 0, false, {
              fileName: _jsxFileName,
              lineNumber: 45,
              columnNumber: 9
            }, void 0)]
          }, void 0, true, {
            fileName: _jsxFileName,
            lineNumber: 43,
            columnNumber: 8
          }, void 0)]
        }, void 0, true, {
          fileName: _jsxFileName,
          lineNumber: 39,
          columnNumber: 7
        }, void 0)
      });
      if (result === true) {
        const entityToConnect = entity.getAccessor();
        connect(entityToConnect);
      } else {
        entity.deleteEntity();
      }
    };
  }, [createNewForm, getSubTree, desugaredOptionPath.entityName, dialog, accessorTreeState, localization, connect]);
};
export {
  useOnAddNew
};
//# sourceMappingURL=useOnAddNew.js.map
