'use strict';
import { jsxs, jsx } from 'react/jsx-runtime';
import { useState } from 'react';
import { Button } from '../../../../fd-component-toolbox/button/Button.js';
import { onInitBrowserDialog, setBrowserDialogResourceDescription, refreshBrowserDialogResources, onBrowserDialogPaging, getBrowserDialogTableId, onItemSelected } from './BrowserDialog.funcs.js';
import { Dialog, DialogTrigger, DialogContent, DialogTitle, DialogHeader, DialogFooter, DialogClose } from '../../../../fd-component-toolbox/dialog/Dialog.js';
import { useAsyncEffect } from '../../../../../fd-toolbox/hooks/use-async-effect.js';
import { createEmptyShortResourceDescriptor } from '../short-resource-descriptor.js';
import { rowHighlightBehaviors } from '../../../../../fd-toolbox/enums/enums.js';
import { ResourceFilterList } from '../../../filtering/resource-filter-list/ResourceFilterList.js';
import { Table } from '../../../table/Table.js';
import { useEmptyRef } from '../../../../../fd-toolbox/hooks/use-empty-ref.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function BrowserDialog({
  propertyMeta,
  currentResourceId,
  closeDialog,
  trigger,
  setDescription,
  excludeResourceIds,
  disabled
}) {
  const [resources, setResources] = useState([]);
  const [uiResourceMeta, setUiResourceMeta] = useState();
  const [oDataConfig, setODataConfig] = useState({});
  const [isOpen, setIsOpen] = useState(false);
  const tableConfig = {
    disableCellNavigationLinks: true
  };
  const resourceFilterListRef = useEmptyRef();
  const getFilters = /* @__PURE__ */__name(() => resourceFilterListRef.current ? resourceFilterListRef.current.getFilters() : {}, "getFilters");
  useAsyncEffect(async () => {
    await onInitBrowserDialog(setUiResourceMeta, propertyMeta, setResources, setODataConfig, oDataConfig, getFilters, excludeResourceIds);
  }, []);
  useAsyncEffect(async () => {
    await setBrowserDialogResourceDescription(resources, currentResourceId, uiResourceMeta?.resourceMeta, setDescription);
  }, [resources, currentResourceId, setDescription, uiResourceMeta?.resourceMeta]);
  const onRefreshResources = /* @__PURE__ */__name(() => uiResourceMeta?.resourceMeta && refreshBrowserDialogResources(setResources, setODataConfig, oDataConfig, getFilters, uiResourceMeta.resourceMeta, excludeResourceIds), "onRefreshResources");
  const onCancel = /* @__PURE__ */__name(() => closeDialog?.(void 0), "onCancel");
  const onPaging = /* @__PURE__ */__name(paging => uiResourceMeta?.resourceMeta && onBrowserDialogPaging(paging, setResources, setODataConfig, oDataConfig, getFilters, uiResourceMeta.resourceMeta, excludeResourceIds), "onPaging");
  const onItemSelected = /* @__PURE__ */__name(item => handleItemSelected(item, setIsOpen, uiResourceMeta, closeDialog), "onItemSelected");
  const onOpenChange = /* @__PURE__ */__name(open => handleOpenChangeLocal(open, setIsOpen, closeDialog), "onOpenChange");
  return /* @__PURE__ */jsxs(Dialog, {
    open: isOpen && !disabled,
    onOpenChange,
    children: [/* @__PURE__ */jsx(DialogTrigger, {
      nativeButton: false,
      className: "inline-flex size-4 items-center justify-center focus:outline-none focus-visible:ring-0",
      render: props => /* @__PURE__ */jsx("div", {
        ...props,
        children: trigger
      })
    }), /* @__PURE__ */jsxs(DialogContent, {
      className: "box-border flex max-h-[90vh] w-full max-w-sm flex-col sm:max-w-lg md:max-w-3xl lg:max-w-4xl",
      children: [/* @__PURE__ */jsx(DialogTitle, {
        className: "absolute -m-px size-px overflow-hidden border-0 p-0 whitespace-nowrap",
        children: propertyMeta.displayName
      }), /* @__PURE__ */jsx(DialogHeader, {
        children: /* @__PURE__ */jsx("h1", {
          className: "text-lg font-bold",
          children: propertyMeta.displayName
        })
      }), uiResourceMeta?.resourceMeta && /* @__PURE__ */jsx("div", {
        className: "flex flex-row items-center justify-start gap-1",
        children: /* @__PURE__ */jsx(ResourceFilterList, {
          ref: resourceFilterListRef,
          uiResourceMeta,
          stateKey: `${ResourceFilterList.displayName}_${uiResourceMeta.resourceMeta.id}_browser`,
          onFilterChange: onRefreshResources,
          onRefreshResources,
          config: {
            showRefreshButton: true,
            showResetButton: false
          }
        })
      }), propertyMeta && resources && /* @__PURE__ */jsx("div", {
        className: "flex size-full overflow-auto",
        children: /* @__PURE__ */jsx(Table, {
          id: getBrowserDialogTableId(),
          items: resources,
          uiResourceMeta,
          onPaging,
          tableConfig: {
            ...tableConfig,
            rowHighlightBehavior: rowHighlightBehaviors.pointer,
            pageSize: 6,
            adaptiveWidth: true
          },
          onRowClick: onItemSelected,
          onRefreshItems: onRefreshResources
        })
      }), /* @__PURE__ */jsxs(DialogFooter, {
        children: [propertyMeta?.required && /* @__PURE__ */jsx(DialogClose, {
          render: /* @__PURE__ */jsx(Button, {
            onClick: onCancel,
            className: "ml-4 min-w-20",
            variant: "outline",
            children: "Cancel"
          })
        }), /* @__PURE__ */jsx(DialogClose, {
          render: /* @__PURE__ */jsx(Button, {
            onClick: () => closeDialog?.(createEmptyShortResourceDescriptor()),
            className: "ml-4 min-w-20",
            variant: "outline",
            children: "Clear"
          })
        })]
      })]
    })]
  });
}
__name(BrowserDialog, "BrowserDialog");
function handleOpenChangeLocal(open, setIsOpen, closeDialog) {
  setIsOpen(open);
  if (!open) closeDialog?.(void 0);
}
__name(handleOpenChangeLocal, "handleOpenChangeLocal");
function handleItemSelected(item, setIsOpen, uiResourceMeta, closeDialog) {
  if (uiResourceMeta?.resourceMeta && closeDialog) {
    onItemSelected(item, uiResourceMeta.resourceMeta, closeDialog);
    setIsOpen(false);
  }
}
__name(handleItemSelected, "handleItemSelected");

export { BrowserDialog };
