import { r as reactExports, j as jsxRuntimeExports } from "./vendor-react-DYNCg6N5.js";
import { M as Modal } from "./index-DqcA8P_y.js";
import { m as modalTheme } from "./styles-3NoIlzov.js";
import { c as createStyles, z as useForm, b as Box, a0 as SimpleGrid, F as Flex, T as Text, k as Tooltip, S as Stack, R as Radio, ad as NumberInput, B as Button, V as LoadingOverlay } from "./vendor-mantine-h0WlM2Mj.js";
import { T as TextFieldInput } from "./index-BvQfhnV_.js";
import { S as SelectQuotas } from "./index-BoQKW1g7.js";
import { c as useAppSelector, bp as useAddQuotaRelationsMutation, bq as useEditQuotaRelationsMutation, m as universalNotification } from "./index-DfebxkL9.js";
import { g as getValidateErrors } from "./validation-Ds00Zghx.js";
import { v as validateQuotaRelationScheme } from "./validate-CA6bwF22.js";
import { g as getEntityTypeFromUrl } from "./utiils-DzEu0_6s.js";
import { b as convertGbToByte } from "./format-CMT-x-V3.js";
import { k as zte } from "./vendor-icons-xWhXK_Ui.js";
import { u as useTranslation } from "./vendor-i18n-wisIXUFi.js";
const useStyles = createStyles((theme) => ({
  modalRoot: modalTheme(theme).root,
  modalContainer: modalTheme(theme).container,
  modalFooter: modalTheme(theme).footer,
  // modalContainer: {
  //   width: '100%',
  //   height: '100%',
  //   display: 'flex',
  //   flexDirection: 'column',
  //   alignItems: 'center',
  //   justifyContent: 'space-between',
  //   background: 'white'
  // },
  input: {
    "& .mantine-TextInput-label": {
      color: theme.colors.brandGray[0],
      fontSize: "12px"
    }
  },
  radio: {
    padding: "5px 0",
    "& .mantine-Radio-inner": {
      alignSelf: "center"
    },
    "& .mantine-Radio-label": {
      fontSize: "14px",
      paddingLeft: theme.spacing.xs
    }
  },
  numberInput: {
    "&  .mantine-InputWrapper-label": {
      color: theme.colors.brandGray[0],
      fontSize: "12px"
    },
    "& .mantine-NumberInput-input": {
      borderRadius: "8px",
      border: "1px solid #D0D5DD"
    }
  },
  select: {
    "&  .mantine-TextInput-label": {
      color: theme.colors.brandGray[0],
      fontSize: "12px",
      fontWeight: 400
    }
  }
}));
const AssignQuotaModal = ({
  opened,
  handleClose
}) => {
  const {
    t
  } = useTranslation();
  const {
    classes,
    theme
  } = useStyles();
  const {
    filial
  } = useAppSelector((state) => state.filials);
  const [addQuotaRelation, {
    isLoading
  }] = useAddQuotaRelationsMutation();
  const [editQuotaRelation, {
    isLoading: isLoadingEdit
  }] = useEditQuotaRelationsMutation();
  const {
    selectedQuotaRelation
  } = useAppSelector((state_0) => state_0.quotas);
  const isEditModal = reactExports.useMemo(() => !!selectedQuotaRelation, [selectedQuotaRelation]);
  const form = useForm({
    initialValues: {
      CustomerId: null,
      Url: "",
      QuotaId: null,
      QuotaName: "",
      EntityType: null,
      NewQuota: false,
      Quota: null
    },
    validateInputOnChange: true,
    validate: (values) => getValidateErrors(validateQuotaRelationScheme(t), values)
  });
  reactExports.useEffect(() => {
    if (!filial) {
      return;
    }
    const formData = isEditModal && selectedQuotaRelation ? {
      CustomerId: filial.Id,
      QuotaId: selectedQuotaRelation.Quota.Id,
      EntityType: selectedQuotaRelation.Entity.Type,
      NewQuota: false,
      Url: selectedQuotaRelation.Url,
      Quota: {
        Name: "",
        Size: null
      }
    } : {
      CustomerId: filial.Id,
      Url: "",
      QuotaId: null,
      EntityType: null,
      NewQuota: false,
      Quota: {
        Name: "",
        Size: null
      }
    };
    form.setValues(formData);
    form.resetDirty(formData);
  }, [filial, selectedQuotaRelation]);
  const closeModal = () => {
    form.reset();
    handleClose();
  };
  const handleSubmit = async (values_0) => {
    var _a, _b;
    const relationQuotaValues = {
      CustomerId: values_0.CustomerId,
      Url: values_0.Url,
      QuotaId: values_0.NewQuota ? null : values_0.QuotaId,
      EntityType: getEntityTypeFromUrl(values_0.Url),
      Quota: values_0.NewQuota ? {
        Name: (_a = values_0.Quota) == null ? void 0 : _a.Name,
        Size: convertGbToByte((_b = values_0.Quota) == null ? void 0 : _b.Size)
      } : null
    };
    try {
      const res = isEditModal ? await editQuotaRelation(relationQuotaValues) : await addQuotaRelation(relationQuotaValues);
      if ("error" in res) {
        universalNotification({
          error: res.error,
          isError: true,
          isSuccess: false
        });
      } else {
        universalNotification({
          isSuccess: true,
          isError: false,
          error: null,
          successTitle: isEditModal ? t("filials.diskSpace.editQuotaSuccess") : t("filials.diskSpace.assignQuotaSuccess")
        });
        closeModal();
      }
    } catch (error) {
      console.error(error);
    }
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(Modal, { opened, onClose: closeModal, title: isEditModal ? t("filials.diskSpace.editAssign") : t("filials.diskSpace.newAssign"), zIndex: 1300, size: "420px", children: /* @__PURE__ */ jsxRuntimeExports.jsxs("form", { className: classes.modalRoot, onSubmit: form.onSubmit(handleSubmit), children: [
    /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalContainer, children: /* @__PURE__ */ jsxRuntimeExports.jsxs(SimpleGrid, { cols: 1, w: "100%", spacing: "xs", children: [
      /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { form, fieldName: "Url", label: /* @__PURE__ */ jsxRuntimeExports.jsxs(Flex, { gap: "xs", children: [
        t("link"),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { c: "red", children: "*" }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(Tooltip, { w: "450px", multiline: true, zIndex: 2e3, label: t("filials.diskSpace.urlTooltip").split("\n").map((item, index) => /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { size: "sm", children: item }, index)), children: /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { h: 16, children: /* @__PURE__ */ jsxRuntimeExports.jsx(zte, { size: 16, stroke: "2", color: theme.colors.brandGray[0] }) }) })
      ] }), placeholder: t("filials.diskSpace.pastUrl"), dataAttribute: "urlInput", disabled: isEditModal, isRequired: true, withAsterisk: false }),
      /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { gap: "xs", mt: "5px", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(Radio, { checked: !form.values.NewQuota, onChange: () => form == null ? void 0 : form.setFieldValue("NewQuota", false), className: classes.radio, label: t("filials.diskSpace.useExistingQuota"), "data-cy": "useExistingQuotaButton" }),
        !form.values.NewQuota ? /* @__PURE__ */ jsxRuntimeExports.jsx(SelectQuotas, { form, fieldName: "QuotaId", value: form.values.QuotaId, Customer: filial == null ? void 0 : filial.Id, label: t("profile.selectDefaultQuota"), className: classes.select, quotaField: "QuotaName", QuotaName: form.values.QuotaName }) : null,
        /* @__PURE__ */ jsxRuntimeExports.jsx(Radio, { checked: form.values.NewQuota, onChange: () => form == null ? void 0 : form.setFieldValue("NewQuota", true), className: classes.radio, label: t("filials.diskSpace.createNewQuota"), "data-cy": "createNewQuotaButton" })
      ] }),
      form.values.NewQuota ? /* @__PURE__ */ jsxRuntimeExports.jsxs(Stack, { gap: "xs", ml: "30px", children: [
        /* @__PURE__ */ jsxRuntimeExports.jsx(TextFieldInput, { form, fieldName: "Quota.Name", label: t("filials.diskSpace.quotaName"), dataAttribute: "quotaName", isRequired: true }),
        /* @__PURE__ */ jsxRuntimeExports.jsx(NumberInput, { autoComplete: "off", hideControls: true, allowNegative: false, withAsterisk: true, className: classes.numberInput, label: t("filials.diskSpace.quotaSizeInGb"), ...form.getInputProps("Quota.Size") })
      ] }) : null,
      /* @__PURE__ */ jsxRuntimeExports.jsx(Text, { fz: "sm", c: "#667085", mt: "xs", children: t("filials.diskSpace.assignQuotaDescription") })
    ] }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(Box, { className: classes.modalFooter, children: /* @__PURE__ */ jsxRuntimeExports.jsx(Button, { type: "submit", size: "sm", radius: 8, fullWidth: true, disabled: !form.isValid() || !form.isDirty(), children: isEditModal ? t("filials.diskSpace.save") : t("filials.diskSpace.assignQuotas") }) }),
    /* @__PURE__ */ jsxRuntimeExports.jsx(LoadingOverlay, { visible: isLoading || isLoadingEdit, overlayProps: {
      blur: 2
    } })
  ] }) });
};
export {
  AssignQuotaModal as A
};
//# sourceMappingURL=index-DeLPTh4Q.js.map
