'use strict';
'use client';
import { c } from 'react/compiler-runtime';
import { jsx, jsxs } from 'react/jsx-runtime';
import { useState } from 'react';
import { initWorkspacePicker, onSelectedWorkspaceChange, getWorkspacePickerSelectItemId, getWorkspacePickerSelectTriggerId } from './WorkspacePicker.funcs.js';
import { SelectItem, Select, SelectTrigger, SelectValue, SelectContent, SelectGroup } from '../../../fd-component-toolbox/select/Select.js';
import { useAsyncEffect } from '../../../../fd-toolbox/hooks/use-async-effect.js';
import { Icon, iconNames } from '../../../fd-component-toolbox/icon/Icon.js';
import { useOptionalPromise } from '../../../../fd-toolbox/hooks/use-optional-promise.js';

var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", {
  value,
  configurable: true
});
function WorkspacePicker(t0) {
  const $ = c(14);
  const {
    parentContextName,
    userWorkspaces
  } = t0;
  const userWorkspacesValue = useOptionalPromise(userWorkspaces);
  let t1;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
    t1 = [];
    $[0] = t1;
  } else {
    t1 = $[0];
  }
  const [childWorkspaces, setChildWorkspaces] = useState(t1);
  const [homeWorkspace, setHomeWorkspace] = useState();
  const [isOpen, setIsOpen] = useState(false);
  const [selectedWorkspace, setSelectedWorkspace] = useState("");
  let t2;
  if ($[1] !== userWorkspacesValue) {
    t2 = async () => {
      await initWorkspacePicker(setChildWorkspaces, setHomeWorkspace, setSelectedWorkspace, userWorkspacesValue);
    };
    $[1] = userWorkspacesValue;
    $[2] = t2;
  } else {
    t2 = $[2];
  }
  let t3;
  if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
    t3 = [];
    $[3] = t3;
  } else {
    t3 = $[3];
  }
  useAsyncEffect(t2, t3);
  if (childWorkspaces.length === 0) {
    return void 0;
  }
  let t4;
  if ($[4] !== childWorkspaces || $[5] !== homeWorkspace || $[6] !== isOpen || $[7] !== parentContextName || $[8] !== selectedWorkspace) {
    const selectedWorkspaceLabel = selectedWorkspace === homeWorkspace?.id ? homeWorkspace?.name : childWorkspaces.find(workspace => workspace.id === selectedWorkspace)?.name;
    let t5;
    if ($[10] !== selectedWorkspace) {
      t5 = value => onSelectedWorkspaceChange(value ?? void 0, selectedWorkspace);
      $[10] = selectedWorkspace;
      $[11] = t5;
    } else {
      t5 = $[11];
    }
    let t6;
    if ($[12] !== parentContextName) {
      t6 = workspace_0 => jsx(SelectItem, {
        value: workspace_0.id,
        id: getWorkspacePickerSelectItemId(parentContextName ?? "", workspace_0.id),
        children: workspace_0.name
      }, workspace_0.id);
      $[12] = parentContextName;
      $[13] = t6;
    } else {
      t6 = $[13];
    }
    t4 = jsxs(Select, {
      value: selectedWorkspace,
      onValueChange: t5,
      open: isOpen,
      onOpenChange: setIsOpen,
      children: [jsx(SelectTrigger, {
        id: getWorkspacePickerSelectTriggerId(parentContextName ?? ""),
        noUnderline: true,
        className: "hover:bg-muted/50 cursor-pointer p-2 font-medium shadow-none",
        children: jsxs("div", {
          className: "flex items-center gap-x-2",
          children: [jsx(Icon, {
            name: iconNames.desktop,
            className: "size-3"
          }), jsx(SelectValue, {
            children: selectedWorkspaceLabel
          })]
        })
      }), jsx(SelectContent, {
        children: jsxs(SelectGroup, {
          children: [homeWorkspace && jsx(SelectItem, {
            value: homeWorkspace.id,
            id: getWorkspacePickerSelectItemId(parentContextName ?? "", homeWorkspace.id),
            children: homeWorkspace.name
          }), childWorkspaces.map(t6)]
        })
      })]
    });
    $[4] = childWorkspaces;
    $[5] = homeWorkspace;
    $[6] = isOpen;
    $[7] = parentContextName;
    $[8] = selectedWorkspace;
    $[9] = t4;
  } else {
    t4 = $[9];
  }
  return t4;
}
__name(WorkspacePicker, "WorkspacePicker");

export { WorkspacePicker };
