import { r as reactExports, u as useForm, j as jsxRuntimeExports, I as Icon, P as PowerhouseButton, M as Modal, t as twMerge, e as usePHModal, a0 as getDriveSharingType, a1 as getDriveAvailableOffline, q as showPHModal, i as closePHModal, a2 as setDriveSharingType, a3 as setDriveAvailableOffline, a4 as renameDrive } from "./index-DEuI3Aoa.js";
import { u as useDriveById } from "./drive-by-id-DG_S24DX.js";
import { F as FormInput } from "./form-input-DucXwitU.js";
import { D as Divider, L as Label, S as SharingTypeFormInput, a as LocationInfo, A as AvailableOfflineToggle } from "./toggle-OA2woAUh.js";
import { D as Disclosure } from "./disclosure-DtNIZI7D.js";
function DriveSettingsForm(props) {
  const { drive, sharingType, availableOffline, onSubmit, handleDeleteDrive } = props;
  const name = drive.header.name;
  const [showLocationSettings, setShowLocationSettings] = reactExports.useState(false);
  const [showDangerZone, setShowDangerZone] = reactExports.useState(false);
  const [showDeleteDrive, setShowDeleteDrive] = reactExports.useState(false);
  const { register, handleSubmit, control } = useForm({
    mode: "onBlur",
    defaultValues: {
      name,
      sharingType,
      availableOffline
    }
  });
  const location = sharingType === "PUBLIC" ? "SWITCHBOARD" : sharingType;
  return jsxRuntimeExports.jsxs("form", { onSubmit: handleSubmit(onSubmit), children: [jsxRuntimeExports.jsx(DriveNameInput, { ...register("name") }), jsxRuntimeExports.jsx(Divider, { className: "my-4" }), jsxRuntimeExports.jsx(Label, { htmlFor: "sharingType", children: "Sharing settings" }), jsxRuntimeExports.jsx(SharingTypeFormInput, { control }), jsxRuntimeExports.jsx(Divider, { className: "my-3" }), jsxRuntimeExports.jsxs(Disclosure, { isOpen: showLocationSettings, onOpenChange: () => setShowLocationSettings(!showLocationSettings), title: "Location", children: [jsxRuntimeExports.jsx(LocationInfo, { location }), jsxRuntimeExports.jsx(AvailableOfflineToggle, { ...register("availableOffline") })] }), jsxRuntimeExports.jsx(Divider, { className: "my-3" }), jsxRuntimeExports.jsx(Disclosure, { isOpen: showDangerZone, onOpenChange: () => setShowDangerZone(!showDangerZone), title: "Danger zone", children: jsxRuntimeExports.jsxs("button", { className: "flex gap-2 py-3 font-semibold text-red-900 transition hover:brightness-125", onClick: () => setShowDeleteDrive(true), type: "button", children: [jsxRuntimeExports.jsx(Icon, { name: "Trash" }), "Delete drive"] }) }), showDeleteDrive && showDangerZone ? jsxRuntimeExports.jsx(DeleteDrive, { drive, handleDeleteDrive, onCancel: () => setShowDeleteDrive(false) }) : jsxRuntimeExports.jsxs(jsxRuntimeExports.Fragment, { children: [jsxRuntimeExports.jsx(Divider, { className: "my-3" }), jsxRuntimeExports.jsx(PowerhouseButton, { className: "mb-4 w-full", type: "submit", children: "Confirm" })] })] });
}
function DeleteDrive(props) {
  const { drive, handleDeleteDrive, onCancel } = props;
  const [driveNameInput, setDriveNameInput] = reactExports.useState("");
  const isAllowedToDelete = driveNameInput === drive.header.name;
  function deleteDrive() {
    if (isAllowedToDelete) {
      handleDeleteDrive();
    }
  }
  return jsxRuntimeExports.jsxs("div", { children: [jsxRuntimeExports.jsx("p", { className: "mb-2 rounded-md bg-slate-50 p-4 text-center text-slate-200", children: "Are you sure you want to delete this drive? All files and subfolders within it will be removed. Do you want to proceed?" }), jsxRuntimeExports.jsx(DriveNameInput, { icon: jsxRuntimeExports.jsx(Icon, { name: "Lock" }), onChange: (event) => setDriveNameInput(event.target.value), placeholder: "Enter drive name...", value: driveNameInput }), jsxRuntimeExports.jsxs("div", { className: "flex gap-3", children: [jsxRuntimeExports.jsx(PowerhouseButton, { className: "w-full", color: "light", onClick: onCancel, children: "Cancel" }), jsxRuntimeExports.jsx(PowerhouseButton, { className: "w-full", color: "red", disabled: !isAllowedToDelete, onClick: deleteDrive, children: "Delete" })] })] });
}
const DriveNameInput = reactExports.forwardRef(function DriveNameInput2(props, ref) {
  return jsxRuntimeExports.jsx(FormInput, { ...props, icon: props.icon ?? jsxRuntimeExports.jsx(Icon, { name: "Drive" }), id: "driveName", placeholder: "Drive name", ref });
});
function DriveSettingsModal$1(props) {
  const { drive, open, sharingType, availableOffline, onOpenChange, onDeleteDrive, onRenameDrive, onChangeSharingType, onChangeAvailableOffline, modalProps, containerProps } = props;
  const onSubmit = (data) => {
    if (data.name !== drive.header.name) {
      onRenameDrive(drive, data.name);
    }
    if (data.sharingType !== sharingType) {
      onChangeSharingType(drive, data.sharingType);
    }
    if (data.availableOffline !== availableOffline) {
      onChangeAvailableOffline(drive, data.availableOffline);
    }
    onOpenChange(false);
  };
  function handleDeleteDrive() {
    onDeleteDrive(drive);
    onOpenChange(false);
  }
  function handleCancel() {
    onOpenChange(false);
  }
  return jsxRuntimeExports.jsx(Modal, { ...modalProps, contentProps: {
    className: "rounded-2xl"
  }, onOpenChange, open, children: jsxRuntimeExports.jsxs("div", { ...containerProps, className: twMerge("max-w-[408px] rounded-2xl p-6", containerProps?.className), children: [jsxRuntimeExports.jsxs("div", { className: "flex justify-between", children: [jsxRuntimeExports.jsx("h1", { className: "text-xl font-bold", children: "Drive settings" }), jsxRuntimeExports.jsx("button", { className: "flex size-8 items-center justify-center rounded-md bg-gray-100 text-gray-500 outline-none hover:text-gray-900", onClick: handleCancel, tabIndex: -1, children: jsxRuntimeExports.jsx(Icon, { name: "XmarkLight", size: 24 }) })] }), jsxRuntimeExports.jsx(Divider, { className: "my-4" }), jsxRuntimeExports.jsx(DriveSettingsForm, { handleCancel, handleDeleteDrive, onSubmit, drive, sharingType, availableOffline })] }) });
}
function DriveSettingsModal() {
  const phModal = usePHModal();
  const open = phModal?.type === "driveSettings";
  const driveId = open ? phModal.driveId : void 0;
  const [drive] = useDriveById(driveId);
  const sharingType = getDriveSharingType(drive);
  const availableOffline = getDriveAvailableOffline(drive);
  if (!driveId || !drive) {
    return null;
  }
  async function onRenameDrive(drive2, newName) {
    await renameDrive(drive2.header.id, newName);
  }
  async function onChangeSharingType(drive2, newSharingType) {
    await setDriveSharingType(drive2.header.id, newSharingType);
  }
  async function onChangeAvailableOffline(drive2, newAvailableOffline) {
    await setDriveAvailableOffline(drive2.header.id, newAvailableOffline);
  }
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
    DriveSettingsModal$1,
    {
      drive,
      sharingType: sharingType ?? "LOCAL",
      availableOffline,
      open,
      onRenameDrive,
      onDeleteDrive: () => showPHModal({ type: "deleteDrive", driveId }),
      onChangeAvailableOffline,
      onChangeSharingType,
      onOpenChange: (status) => {
        if (!status) return closePHModal();
      }
    }
  );
}
export {
  DriveSettingsModal
};
//# sourceMappingURL=DriveSettingsModal-34XhefR3.js.map
