import { QueryClient, useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { createToaster, useDisclosure, Dialog, Portal, Flex, Box, Button, CloseButton } from '@chakra-ui/react';
import { jsx, jsxs } from 'react/jsx-runtime';
import { createContext, useContext, useState, useRef, useCallback } from 'react';
import { track as track$1 } from '@amplitude/unified';

// recce-source/js/src/lib/api/cacheKeys.ts
var cacheKeys = {
  rowCount: (model) => ["row_count", model],
  lineage: () => ["lineage"],
  checks: () => ["checks", "list"],
  check: (checkId) => ["checks", checkId],
  checkEvents: (checkId) => ["checks", checkId, "events"],
  runs: () => ["runs"],
  run: (runId) => ["runs", runId],
  runsAggregated: () => ["runs_aggregated"],
  flag: () => ["flag"],
  instanceInfo: () => ["instance_info"],
  user: () => ["user"]
};

// recce-source/js/src/lib/const.ts
var apiUrl = process.env.NEXT_PUBLIC_API_URL;
apiUrl ??= typeof window !== "undefined" ? window.location.origin : "";
var PUBLIC_API_URL = apiUrl;
process.env.NEXT_PUBLIC_CLOUD_WEB_URL;

// recce-source/js/src/lib/api/axiosClient.ts
var axiosClient = axios.create({
  baseURL: PUBLIC_API_URL
});
new QueryClient();

// recce-source/js/src/lib/api/instanceInfo.ts
async function getRecceInstanceInfo() {
  return (await axiosClient.get(
    "/api/instance-info"
  )).data;
}

// recce-source/js/src/lib/hooks/useRecceInstanceInfo.tsx
var useRecceInstanceInfo = () => {
  return useQuery({
    queryKey: cacheKeys.instanceInfo(),
    queryFn: getRecceInstanceInfo
  });
};
var toaster = createToaster({
  placement: "bottom-end",
  pauseOnPageIdle: true
});

// recce-source/js/src/lib/hooks/useCheckToast.tsx
function useCheckToast() {
  function markedAsApprovedToast() {
    toaster.create({
      title: "Marked as approved",
      type: "success",
      duration: 2e3
    });
  }
  return {
    markedAsApprovedToast
  };
}

// recce-source/js/src/lib/hooks/useClipBoardToast.tsx
function useClipBoardToast() {
  function successToast(message) {
    toaster.create({
      description: message,
      type: "info",
      duration: 5e3,
      closable: true
    });
  }
  function failToast(title, error) {
    toaster.create({
      title,
      description: String(error),
      type: "error",
      duration: 5e3,
      closable: true
    });
  }
  return {
    successToast,
    failToast
  };
}
var LineageViewContext = createContext(void 0);
var useLineageViewContext = () => {
  return useContext(LineageViewContext);
};
function track(eventInput, eventProperties, eventOptions) {
  {
    console.log("[Tracking]", eventInput, eventProperties, eventOptions);
  }
  return track$1(eventInput, eventProperties, eventOptions);
}
var EXPLORE_ACTION = {
  VALUE_DIFF: "value_diff"};
var EXPLORE_FORM_EVENT = {
  EXECUTE: "execute",
  CANCEL: "cancel"
};
function trackExploreActionForm(props) {
  track("[Web] explore_action_form", props);
}
function useValueDiffAlertDialog() {
  const { open, onOpen, onClose } = useDisclosure();
  const [nodeCount, setNodeCount] = useState(0);
  const [resolvePromise, setResolvePromise] = useState();
  const cancelRef = useRef(null);
  const confirm = useCallback(
    (nodeCount2) => {
      setNodeCount(nodeCount2);
      return new Promise((resolve) => {
        setResolvePromise(() => resolve);
        onOpen();
      });
    },
    [onOpen]
  );
  const handleConfirm = () => {
    trackExploreActionForm({
      action: EXPLORE_ACTION.VALUE_DIFF,
      event: EXPLORE_FORM_EVENT.EXECUTE
    });
    resolvePromise?.(true);
    onClose();
  };
  const handleCancel = () => {
    trackExploreActionForm({
      action: EXPLORE_ACTION.VALUE_DIFF,
      event: EXPLORE_FORM_EVENT.CANCEL
    });
    resolvePromise?.(false);
    onClose();
  };
  const ValueDiffAlertDialog = /* @__PURE__ */ jsx(
    Dialog.Root,
    {
      size: "xl",
      open,
      role: "alertdialog",
      initialFocusEl: () => {
        return cancelRef.current;
      },
      onOpenChange: handleCancel,
      children: /* @__PURE__ */ jsxs(Portal, { children: [
        /* @__PURE__ */ jsx(Dialog.Backdrop, {}),
        /* @__PURE__ */ jsx(Dialog.Positioner, { children: /* @__PURE__ */ jsxs(Dialog.Content, { children: [
          /* @__PURE__ */ jsx(Dialog.Header, { fontSize: "lg", fontWeight: "bold", children: /* @__PURE__ */ jsxs(Dialog.Title, { children: [
            "Value Diff on ",
            nodeCount,
            " nodes"
          ] }) }),
          /* @__PURE__ */ jsx(Dialog.Body, { gap: "20px", as: Flex, direction: "column", children: /* @__PURE__ */ jsxs(Box, { children: [
            "Value diff will be executed on ",
            nodeCount,
            " nodes in the Lineage, which can add extra costs to your bill."
          ] }) }),
          /* @__PURE__ */ jsxs(Dialog.Footer, { gap: 1, children: [
            /* @__PURE__ */ jsx(
              Button,
              {
                ref: cancelRef,
                onClick: handleCancel,
                variant: "outline",
                colorPalette: "gray",
                children: "Cancel"
              }
            ),
            /* @__PURE__ */ jsx(Button, { colorPalette: "iochmara", onClick: handleConfirm, ml: 3, children: "Execute" })
          ] }),
          /* @__PURE__ */ jsx(Dialog.CloseTrigger, { asChild: true, children: /* @__PURE__ */ jsx(CloseButton, { size: "sm" }) })
        ] }) })
      ] })
    }
  );
  return { confirm, AlertDialog: ValueDiffAlertDialog };
}
var useValueDiffAlertDialog_default = useValueDiffAlertDialog;

export { useCheckToast, useClipBoardToast, useLineageViewContext, useRecceInstanceInfo, useValueDiffAlertDialog_default as useValueDiffAlertDialog };
//# sourceMappingURL=hooks.mjs.map
//# sourceMappingURL=hooks.mjs.map