'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var useConfirmModal = require('./useConfirmModal.cjs');
var Button = require('../Button/Button.cjs');
var react = require('@chakra-ui/react');
require('chakra-react-select');
require('react-icons/fa');
require('../../styles/theme.cjs');
require('react-icons/bs');
require('react-router-dom');
require('react-icons/md');
require('@chakra-ui/icons');
var utils = require('../../utils/utils.cjs');
require('../Form/FormProvider.cjs');
require('react-hook-form');
require('@hookform/devtools');
require('@hookform/resolvers/yup');
require('yup');
var FormField = require('../FormField/FormField.cjs');
require('../Inputs/Slider/chakra/ChakraTooltip.cjs');
require('react-icons/io');
require('../CodeBlock/CodeBlock.cjs');
require('../DataTable/DataTable.cjs');
require('../../styles/colors.cjs');
require('date-fns/locale/en-US');
require('date-fns-tz/zonedTimeToUtc');
require('react-datepicker');
require('date-fns');
require('date-fns-tz');
require('../Inputs/DateTimeInput/Calendar.cjs');
require('../Popover/PopoverContent.cjs');
require('../Popover/Popover.cjs');
require('../Badge/Badge.cjs');
require('react-icons/io5');
require('@textea/json-viewer');
require('react-markdown');
require('remark-gfm');
require('remark-emoji');

var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
const DEFAULT_VALUES = {
  LABEL_DELETE: "Delete",
  LABEL_CANCEL: "Cancel",
  INPUT_MATCH_TEXT: "delete",
  // input value must match this text to enable the delete button
  TEXT_DELETE_INSTRUCTIONS: `This action will cause data loss. To confirm, type "%input_match_text%" into the confirmation box below.`,
  NOOP: utils.noop
};
const ConfirmItemDeleteModal = (props) => {
  const {
    trigger,
    itemType,
    primaryActionLabel,
    secondaryActionLabel,
    inputMatchText = DEFAULT_VALUES.INPUT_MATCH_TEXT,
    onConfirm,
    onClose = DEFAULT_VALUES.NOOP,
    heading = `${DEFAULT_VALUES.LABEL_DELETE} ${itemType}`,
    children = /* @__PURE__ */ jsxRuntime.jsx(react.Text, { children: DEFAULT_VALUES.TEXT_DELETE_INSTRUCTIONS.replace(/%input_match_text%/, inputMatchText) }),
    requireValidation = true
  } = props;
  const [valid, setValid] = React.useState("");
  const [isDeleting, setIsDeleting] = React.useState(false);
  const [deleteErrorMessage, setDeleteErrorMessage] = React.useState(null);
  const onError = React.useCallback((msg) => {
    setIsDeleting(false);
    setDeleteErrorMessage(msg);
  }, []);
  const resetState = React.useCallback(() => {
    setValid("");
    setIsDeleting(false);
    setDeleteErrorMessage(null);
  }, []);
  const onCancel = React.useCallback(() => {
    onClose();
  }, [onClose]);
  const handleOnClick = React.useCallback(() => __async(void 0, null, function* () {
    resetState();
    yield showModal().catch(() => onCancel());
  }), []);
  const getTriggerElement = React.useCallback(() => {
    if (typeof trigger === "string") {
      return /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "delete", "data-testid": "trigger-delete-button", onClick: handleOnClick, children: trigger });
    } else {
      return React.cloneElement(trigger, {
        onClick: (event) => {
          var _a, _b;
          (_b = (_a = trigger.props).onClick) == null ? void 0 : _b.call(_a, event);
          handleOnClick();
        }
      });
    }
  }, [handleOnClick, trigger]);
  const getPrimaryButtonLabel = React.useCallback(() => {
    return primaryActionLabel || DEFAULT_VALUES.LABEL_DELETE;
  }, [primaryActionLabel]);
  const getSecondaryButtonLabel = React.useCallback(() => {
    return secondaryActionLabel || DEFAULT_VALUES.LABEL_CANCEL;
  }, [secondaryActionLabel]);
  const inputTextDoesNotMatch = React.useCallback((text) => {
    return text.toLowerCase() !== inputMatchText.toLowerCase();
  }, []);
  const { showModal, renderModal } = useConfirmModal({
    heading,
    render: ({ confirm, cancel }) => {
      return /* @__PURE__ */ jsxRuntime.jsxs(react.Stack, { spacing: "4", children: [
        children,
        /* @__PURE__ */ jsxRuntime.jsx(
          FormField,
          {
            label: "",
            isInvalid: valid !== "" && inputTextDoesNotMatch(valid),
            errorText: `Text must match "${inputMatchText}"`,
            children: /* @__PURE__ */ jsxRuntime.jsx(
              react.Input,
              {
                type: "text",
                name: "name",
                "data-testid": "txt-confirmation-delete",
                placeholder: `Type "${inputMatchText}" to confirm`,
                onChange: (v) => setValid(v.target.value)
              }
            )
          }
        ),
        deleteErrorMessage && /* @__PURE__ */ jsxRuntime.jsx(react.Text, { color: "error.500", children: deleteErrorMessage }),
        /* @__PURE__ */ jsxRuntime.jsx(react.Box, { alignSelf: "end", children: /* @__PURE__ */ jsxRuntime.jsxs(react.ButtonGroup, { children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            Button,
            {
              variant: "delete",
              "data-testid": `test-delete-item`,
              id: "delete-modal-btn",
              onClick: () => {
                setIsDeleting(true);
                setDeleteErrorMessage(null);
                onConfirm(confirm, onError);
              },
              isLoading: isDeleting,
              isDisabled: props.isDisabled || valid === "" || requireValidation && inputTextDoesNotMatch(valid),
              children: getPrimaryButtonLabel()
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(Button, { variant: "ghost", "data-testid": `test-cancel-item`, onClick: cancel, children: getSecondaryButtonLabel() })
        ] }) })
      ] });
    }
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    getTriggerElement(),
    renderModal()
  ] });
};

module.exports = ConfirmItemDeleteModal;
//# sourceMappingURL=ConfirmItemDeleteModal.cjs.map
