'use strict';
import { c } from 'react/compiler-runtime';
import { jsxs, jsx } from 'react/jsx-runtime';
import { Sheet, SheetTrigger, SheetContent, SheetHeader, SheetDescription, SheetTitle } from '../../fd-component-toolbox/sheet/Sheet.js';
import React, { useState } from 'react';
import { EditResource } from './EditResource.js';
import { emptyUuid } from '../../../fd-toolbox/constants/constants.js';
import { metaQueryParams } from '../../../fd-toolbox/constants/meta-query-params.js';
import { onEditRecourceSheetOpenChange, onSheetClose } from './EditResourceSheet.funcs.js';
import { useSearchParams } from '../client/framework/hooks.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';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function EditResourceSheet(t0) {
  const $ = c(10);
  const {
    id: propId,
    resourceName,
    children,
    onEditClick,
    onRefreshResources,
    onCloseClick
  } = t0;
  const [isOpen, setIsOpen] = useState(false);
  const params = useSearchParams();
  const paramsId = params.get(metaQueryParams.id);
  let t1;
  if ($[0] !== params || $[1] !== resourceName) {
    t1 = resourceName ?? params.get(metaQueryParams.resource) ?? "";
    $[0] = params;
    $[1] = resourceName;
    $[2] = t1;
  } else {
    t1 = $[2];
  }
  const resolvedResourceName = t1;
  const resourceId = isUuidSet(propId) && propId || isUuidSet(paramsId) && paramsId || emptyUuid;
  const [uiResourceMeta, setUiResourceMeta] = useState();
  const isNew = !isUuidSet(resourceId);
  let t2;
  if ($[3] !== resolvedResourceName) {
    t2 = async () => {
      setUiResourceMeta(await fetchUiResourceMeta(resolvedResourceName));
    };
    $[3] = resolvedResourceName;
    $[4] = t2;
  } else {
    t2 = $[4];
  }
  useAsyncEffect(t2);
  let t3;
  if ($[5] !== onCloseClick || $[6] !== onEditClick || $[7] !== propId || $[8] !== resolvedResourceName) {
    t3 = open => onEditRecourceSheetOpenChange(open, resolvedResourceName, setIsOpen, propId, onEditClick, "", onCloseClick);
    $[5] = onCloseClick;
    $[6] = onEditClick;
    $[7] = propId;
    $[8] = resolvedResourceName;
    $[9] = t3;
  } else {
    t3 = $[9];
  }
  const onOpenChange = __name(t3, "onOpenChange");
  return jsxs(Sheet, {
    open: isOpen,
    onOpenChange,
    children: [jsx(SheetTrigger, {
      render: React.isValidElement(children) ? children : jsx("span", {
        children
      })
    }), jsxs(SheetContent, {
      className: "w-full overflow-y-auto sm:w-2/4 lg:w-2/6 xl:w-1/4",
      children: [jsx(SheetHeader, {
        children: jsx(SheetDescription, {})
      }), jsx(SheetTitle, {
        className: "absolute -m-px size-px overflow-hidden border-0 p-0 whitespace-nowrap text-transparent",
        children: "SheetContent"
      }), jsx("div", {
        children: uiResourceMeta && jsx(EditResource, {
          uiResourceMeta,
          resourceId,
          isNew,
          onSaveClick: () => onSheetClose(onOpenChange, onCloseClick),
          onSaveComplete: () => onRefreshResources?.(),
          onDeleteComplete: () => onSheetClose(onOpenChange, onCloseClick),
          onCancelClick: () => onSheetClose(onOpenChange, onCloseClick),
          onRefreshResource: onRefreshResources
        })
      })]
    })]
  });
}
__name(EditResourceSheet, "EditResourceSheet");

export { EditResourceSheet };
