"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const ahooks = require("ahooks");
const React = require("react");
const index$1 = require("../../icons/index.cjs");
;/* empty css                                                                                                                                                                                        */
;/* empty css                                                                                                                                                                                         */
;/* empty css                                                                                                                                                                                                 */
;/* empty css                                                                                                                                                                                            */
;/* empty css                                                                                                                                                                                            */
const index = require("../Tree/index.cjs");
const Card = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Card/Card.cjs");
const Box = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/core/Box/Box.cjs");
const Stack = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Stack/Stack.cjs");
const ActionIcon = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/ActionIcon/ActionIcon.cjs");
const createStyles = require("../../node_modules/.pnpm/@mantine_emotion@7.15.2_@emotion_cache@11.14.0_@emotion_react@11.14.0_@types_react@18.3.18_re_vflj3eioazr6wkrxpatinf66nq/node_modules/@mantine/emotion/esm/create-styles.cjs");
const useStyles = createStyles.createStyles({
  transferTree: {
    display: "flex",
    flexDirection: "row",
    gap: "2rem",
    alignItems: "stretch"
  },
  transfer: {
    flexBasis: 400,
    borderRadius: 8,
    overflow: "hidden",
    display: "flex",
    flexDirection: "column"
  },
  treeWrapper: {
    padding: "1rem",
    flex: 1,
    overflow: "auto"
  }
});
const TransferTree = ({
  sourceHeader,
  targetHeader,
  onChange,
  dataSource,
  className,
  treeProps = {},
  onCheckLeft,
  onCheckRight,
  onMoveRight,
  onMoveLeft,
  initialTargetKeys
}) => {
  const { classes, cx } = useStyles();
  const [source, setSource] = React.useState(dataSource);
  const [checkedSource, setCheckedSource] = React.useState(/* @__PURE__ */ new Set());
  const [checkedTarget, setCheckedTarget] = React.useState(/* @__PURE__ */ new Set());
  const [selectedNodeKeys, setSelectedNodeKeys] = React.useState(new Set(initialTargetKeys ?? []));
  const renderedSource = React.useMemo(() => {
    return filterTree(
      source,
      () => true,
      (node) => selectedNodeKeys.has(node.key) ? { ...node, disabled: true } : node
    );
  }, [source, selectedNodeKeys]);
  const renderedTarget = React.useMemo(() => {
    return filterTree(
      source,
      (node) => selectedNodeKeys.has(node.key) || (node.children ? node.children.some((c) => selectedNodeKeys.has(c.key)) : false)
    );
  }, [source, selectedNodeKeys]);
  const onMoveToRight = () => {
    setSelectedNodeKeys(new Set(checkedSource));
    onMoveRight == null ? void 0 : onMoveRight();
  };
  const onMoveToLeft = () => {
    setSelectedNodeKeys((prev) => new Set([...prev].filter((i) => !checkedTarget.has(i))));
    onMoveLeft == null ? void 0 : onMoveLeft();
  };
  const onCheckLeftTree = (selected, event) => {
    setCheckedSource(/* @__PURE__ */ new Set([...selected, ...event.halfCheckedKeys]));
    onCheckLeft == null ? void 0 : onCheckLeft(selected, event);
  };
  const onCheckRightTree = (selected, event) => {
    setCheckedTarget(new Set(selected));
    onCheckRight == null ? void 0 : onCheckRight(selected, event);
  };
  ahooks.useUpdateEffect(() => {
    setSource(dataSource);
  }, [dataSource]);
  ahooks.useUpdateEffect(() => {
    onChange == null ? void 0 : onChange(renderedTarget);
  }, [renderedTarget]);
  ahooks.useMount(() => {
    if (initialTargetKeys == null ? void 0 : initialTargetKeys.length) {
      setCheckedSource(/* @__PURE__ */ new Set([...initialTargetKeys]));
    }
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(Card.Card, { withBorder: true, className: cx(classes.transferTree, className), children: [
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classes.transfer, children: [
      sourceHeader ? sourceHeader : /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { component: "header", p: 16, sx: (theme) => ({ backgroundColor: theme.colors.carbon[3] }), children: "Source" }),
      /* @__PURE__ */ jsxRuntime.jsx("div", { className: classes.treeWrapper, children: /* @__PURE__ */ jsxRuntime.jsx(
        index.Tree,
        {
          ...treeProps,
          checkable: true,
          treeData: renderedSource,
          onCheck: ahooks.useMemoizedFn(onCheckLeftTree),
          defaultCheckedKeys: initialTargetKeys
        }
      ) })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs(Stack.Stack, { align: "center", justify: "center", children: [
      /* @__PURE__ */ jsxRuntime.jsx(ActionIcon.ActionIcon, { variant: "default", disabled: checkedSource.size === 0, onClick: ahooks.useMemoizedFn(onMoveToRight), children: /* @__PURE__ */ jsxRuntime.jsx(index$1.IconChevronRight, { size: 14 }) }),
      /* @__PURE__ */ jsxRuntime.jsx(ActionIcon.ActionIcon, { variant: "default", disabled: checkedTarget.size === 0, onClick: ahooks.useMemoizedFn(onMoveToLeft), children: /* @__PURE__ */ jsxRuntime.jsx(index$1.IconChevronLeft, { size: 14 }) })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs("div", { className: classes.transfer, children: [
      targetHeader ? targetHeader : /* @__PURE__ */ jsxRuntime.jsx(Box.Box, { component: "header", h: 60, p: 16, sx: (theme) => ({ backgroundColor: theme.colors.carbon[3] }), children: "Target" }),
      /* @__PURE__ */ jsxRuntime.jsx("div", { className: classes.treeWrapper, children: /* @__PURE__ */ jsxRuntime.jsx(index.Tree, { ...treeProps, checkable: true, treeData: renderedTarget, onCheck: ahooks.useMemoizedFn(onCheckRightTree) }) })
    ] })
  ] });
};
function filterTree(tree, filterFunc, updaterFunc = (i) => i) {
  return tree.filter(filterFunc).map((i) => ({ ...updaterFunc(i), children: i.children ? filterTree(i.children, filterFunc, updaterFunc) : [] }));
}
exports.TransferTree = TransferTree;
