'use strict';
'use client';
import { jsx, jsxs } from 'react/jsx-runtime';
import { Sheet, SheetTrigger, SheetContent, SheetHeader, SheetDescription, SheetTitle } from '../../fd-component-toolbox/sheet/Sheet.js';
import { useState, useEffect } from 'react';
import { emptyUuid } from '../../../fd-toolbox/constants/constants.js';
import { metaQueryParams } from '../../../fd-toolbox/constants/meta-query-params.js';
import { EditResource } from '../edit-resource/EditResource.js';
import { onSheetClose, handleOpenChange, handleSaveComplete, handleDeleteClick, getViewSheetEditButtonId, handleCancelClick } from './ViewSheet.funcs.js';
import { useAsyncEffect } from '../../../fd-toolbox/hooks/use-async-effect.js';
import { fetchUiResourceMeta } from '../ui-meta-providers.js';
import { isUuidSet } from '../../../fd-toolbox/functions/value-checking-functions.js';
import { shouldShowTabs } from '../edit-resource/services/edit-resources.js';
import { cn } from '../../../fd-toolbox/lib/utils.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function ViewSheet({
  id: propId,
  resourceName,
  children,
  onEditClick,
  onRefreshItems,
  onCloseClick,
  uiResourceMeta: uiResourceMetaProp
}) {
  const [isOpen, setIsOpen] = useState(false);
  const [relatedResourceId, setRelatedResourceId] = useState();
  const [relatedResourceName, setRelatedResourceName] = useState();
  const [uiResourceMeta, setUiResourceMeta] = useState();
  const enhanceParams = /* @__PURE__ */__name(search => new URLSearchParams(search), "enhanceParams");
  const params = enhanceParams(window.location.search);
  const paramsId = params.get(metaQueryParams.id) ?? void 0;
  const paramsResourceName = params.get(metaQueryParams.resource) ?? void 0;
  const resolvedResourceName = resourceName ?? params.get(metaQueryParams.resource) ?? "";
  const [resourceId, setResourceId] = useState(isUuidSet(propId) && propId || emptyUuid);
  const isNew = !isUuidSet(resourceId);
  const defaultData = params.get(metaQueryParams.default);
  const defaultDataParams = new URLSearchParams(defaultData ?? "");
  const defaultDataName = defaultDataParams.get(metaQueryParams.relatedResourceName);
  const defaultDataValue = defaultDataParams.get(metaQueryParams.relatedResourceId);
  useAsyncEffect(async () => {
    let uiMeta = uiResourceMetaProp;
    uiMeta ??= await fetchUiResourceMeta(resolvedResourceName);
    setUiResourceMeta(uiMeta);
  });
  useEffect(() => {
    onSheetClose(setResourceId, propId, isOpen);
  }, [isOpen, propId, setResourceId]);
  useEffect(() => {
    if (defaultDataName && defaultDataValue) {
      setRelatedResourceName(defaultDataName);
      setRelatedResourceId(defaultDataValue);
    }
  }, [defaultDataName, defaultDataValue]);
  const onOpenChange = /* @__PURE__ */__name(async open => handleOpenChange(open, setIsOpen, resolvedResourceName, paramsId ?? void 0, paramsResourceName ?? void 0, propId, onEditClick, onCloseClick), "onOpenChange");
  const onSaveComplete = /* @__PURE__ */__name(async (open_0, savedResourceId) => handleSaveComplete(savedResourceId, setResourceId, setIsOpen, onOpenChange, onRefreshItems, onCloseClick, open_0), "onSaveComplete");
  const onDeleteComplete = /* @__PURE__ */__name(() => handleDeleteClick(onOpenChange, onCloseClick), "onDeleteComplete");
  let hasShowTabs = false;
  if (uiResourceMeta) {
    hasShowTabs = shouldShowTabs(uiResourceMeta);
  }
  return /* @__PURE__ */jsx("div", {
    onClick: e => e.stopPropagation(),
    children: /* @__PURE__ */jsxs(Sheet, {
      open: isOpen,
      onOpenChange,
      children: [/* @__PURE__ */jsx(SheetTrigger, {
        nativeButton: false,
        className: "cursor-pointer",
        id: getViewSheetEditButtonId(propId),
        render: children
      }), /* @__PURE__ */jsxs(SheetContent, {
        className: cn("overflow-y-auto py-8 pl-2 sm:pl-6", hasShowTabs ? "w-full pr-0 sm:w-5/6 lg:w-4/5 xl:w-3/4 2xl:w-2/3" : "w-full pr-4 sm:w-2/3 lg:w-7/12 xl:w-2/5"),
        children: [/* @__PURE__ */jsx(SheetHeader, {
          children: /* @__PURE__ */jsx(SheetDescription, {})
        }), /* @__PURE__ */jsx(SheetTitle, {
          className: "absolute -m-px size-px overflow-hidden border-0 p-0 whitespace-nowrap text-transparent",
          children: "SheetContent"
        }), /* @__PURE__ */jsx("div", {
          className: "h-full",
          children: isOpen && uiResourceMeta && /* @__PURE__ */jsx(EditResource, {
            resourceId,
            onSaveComplete,
            onCancelClick: () => handleCancelClick(onOpenChange, onCloseClick),
            onDeleteComplete,
            onRefreshResource: onRefreshItems,
            uiResourceMeta,
            isNew,
            relatedResourceId,
            relatedResourceName
          })
        })]
      })]
    })
  });
}
__name(ViewSheet, "ViewSheet");

export { ViewSheet };
