#!/usr/bin/env node
import { Command } from "commander";
import { writeFileSync, existsSync, rmSync, readFileSync } from "node:fs";
import path, { join, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
import { execa } from "execa";
const deprecated = [
  "Accordion",
  "AccordionHeaderIcon",
  "AccordionHeaderProps",
  "AccordionItem",
  "ActionMenuProps",
  "Badge",
  "BadgeEmphasis",
  "BadgeProps",
  "BadgeStatus",
  "BadgeStyle",
  "Breadcrumb",
  "Breadcrumbs",
  "CollapsibleWrap",
  "Color",
  "FOCUS_VISIBLE_STYLE",
  "FOCUS_VISIBLE_STYLE_INSET",
  "FOCUS_WITHIN_STYLE",
  "FormControl",
  "FormControlDirection",
  "FormControlProps",
  "FormControlStyle",
  "FrontifyPattern",
  "Heading",
  "HelperPosition",
  "LegacyTextarea",
  "LegacyTextareaProps",
  "MenuItem",
  "MenuItemContentSize",
  "MenuItemProps",
  "MenuItemStyle",
  "MenuItemType",
  "Palette",
  "PatternDesign",
  "PatternScale",
  "PatternTheme",
  "RadioButton",
  "RadioList",
  "RadioListDirection",
  "TABBABLE_ELEMENTS",
  "TabItem",
  "TabItemProps",
  "TabSize",
  "Table",
  "Tabs",
  "Text",
  "Textarea",
  "Toast",
  "ToastAnimationDirection",
  "ToastStyle",
  "Validation",
  "merge",
  "useCopy",
  "useForwardedRef",
  "useMemoizedId",
  "useMobileDetection"
];
const active = [
  "AssetInput",
  "AssetInputMenuBlock",
  "AssetInputMenuDefaultItemType",
  "AssetInputMenuItemContentProps",
  "AssetInputMenuItemContentSize",
  "AssetInputMenuItemProps",
  "AssetInputMenuItemStyle",
  "AssetInputMenuItemType",
  "AssetInputMenuSwitchItemType",
  "AssetInputProps",
  "AssetInputSelectionIndicatorIcon",
  "AssetInputSize",
  "AssetType",
  "CollapsibleWrap",
  "CollisionPosition",
  "ComponentEnum",
  "DatePicker",
  "DatePickerProps",
  "DatePickerTrigger",
  "DragHandle",
  "DragHandleProps",
  "DragHandlerPosition",
  "EXPAND_ONHOVER_DELAY",
  "EditableMode",
  "EditableText",
  "EditableTextProps",
  "ExpandButton",
  "ExpandButtonProps",
  "FilterableMultiSelect",
  "FilterableMultiSelect",
  "FilterableMultiSelectItem",
  "FilterableMultiSelectProps",
  "FilterableMultiSelectSize",
  "FilterableMultiSelectType",
  "INDENTATION_WIDTH",
  "ImageAsset",
  "InputLabelProps",
  "InternalTreeItemMultiSelectProps",
  "InternalTreeItemProps",
  "Item",
  "LegacyTextarea",
  "LibrarySource",
  "MultiSelect",
  "MultiSelect",
  "MultiSelect",
  "MultiSelectItem",
  "MultiSelectProps",
  "MultiSelectSize",
  "MultiSelectType",
  "OnExpandCallback",
  "OnSelectCallback",
  "OnSelectInternalCallback",
  "OnShrinkCallback",
  "OnTreeDropCallback",
  "OrderableList",
  "OrderableList",
  "OrderableListItem",
  "OrderableListItemStyle",
  "OrderableListProps",
  "PatternScaleOrigin",
  "Projection",
  "ProjectionArgs",
  "ROOT_ID",
  "ReactDatePickerRef",
  "RegisterNodeChildrenPayload",
  "RenderListItem",
  "SensorContext",
  "SortableProps",
  "SortableTreeItemProps",
  "Tree",
  "TreeActive",
  "TreeAnnouncements",
  "TreeDragCancelEvent",
  "TreeDragEndEvent",
  "TreeDragMoveEvent",
  "TreeDragOverEvent",
  "TreeDragStartEvent",
  "TreeItem",
  "TreeItemBorderClassMap",
  "TreeItemBorderRadiusClassMap",
  "TreeItemBorderStyleClassMap",
  "TreeItemColors",
  "TreeItemColorsClassMap",
  "TreeItemMultiselect",
  "TreeItemMultiselectProps",
  "TreeItemMultiselectWithNodes",
  "TreeItemOverlay",
  "TreeItemProps",
  "TreeItemPropsSizing",
  "TreeItemShadowClassMap",
  "TreeItemSpacingClassMap",
  "TreeItemState",
  "TreeItemStyling",
  "TreeItemWithContentComponentProps",
  "TreeItemWithLabelProps",
  "TreeNodeWithoutElements",
  "TreeOver",
  "TreeProps",
  "TreeState",
  "TreeStateAction",
  "UploadSource",
  "addSelectedIds",
  "addSelectedItemsFromSelection",
  "cleanOrphanSelectedIds",
  "convertToPartialSelectedId",
  "currentNodesChanged",
  "extractNodeFromElement",
  "findIndexById",
  "fixParentSelectionState",
  "getAnnouncements",
  "getCurrentChildrenForNewNodesIfExpanded",
  "getExtendedId",
  "getMultiselectCheckBoxState",
  "getNewSelectedItems",
  "getNodeChildrenIds",
  "getNodesToRender",
  "getParentSelectedTreeItem",
  "getProjection",
  "getReactNodeIdsInFlatArray",
  "getReactNodesInFlatArray",
  "getSelectedChildrenItems",
  "getSelectedTreeItem",
  "getTreeNodesWithoutElements",
  "getVerticalPositioning",
  "handleKeyDownEvent",
  "removePartialFlagSelectedId",
  "removeReactNodesFromFlatArray",
  "removeSelectedIds",
  "sensorsActivationConstraint",
  "shouldUpdateTreeState",
  "updateNodeWithNewChildren",
  "useTreeItem"
];
const detectedExports = {
  deprecated,
  active
};
const __dirname$1 = dirname(fileURLToPath(import.meta.url));
const require2 = createRequire(import.meta.url);
const transformsDirectory = join(__dirname$1, "transforms");
const jscodeshiftExecutable = require2.resolve(".bin/jscodeshift");
const runCodeshift = async (pathToAnalyze, transformName, options) => {
  const transformPath = join(transformsDirectory, `${transformName}.ts`);
  await execa(
    jscodeshiftExecutable,
    [
      "--parser",
      "tsx",
      "--extensions",
      "ts,tsx,js,jsx",
      "--ignore-pattern",
      "**/node_modules/**",
      "--ignore-pattern",
      "**/dist/**",
      "--ignore-pattern",
      "**/build/**",
      "--ignore-pattern",
      "**/.git/**",
      "--ignore-pattern",
      "**/public/**",
      "--transform",
      transformPath,
      pathToAnalyze
    ],
    {
      env: {
        ...options
      }
    }
  );
  return;
};
const __dirname = dirname(fileURLToPath(import.meta.url));
const tempDir = path.resolve(__dirname, "./temp");
const getImports = (pathToAnalyze, selectedImports = []) => {
  if (existsSync(tempDir)) {
    rmSync(tempDir, { recursive: true });
  }
  return runCodeshift(pathToAnalyze, "find-imports", {
    selectedImports: selectedImports.join(","),
    tempDir
  }).then(() => {
    if (existsSync(join(tempDir, "detected-imports.txt"))) {
      const detectedImports = readFileSync(join(tempDir, "detected-imports.txt"), "utf8").split("\n").filter((line) => line.trim() !== "").map((line) => JSON.parse(line));
      rmSync(join(tempDir), { recursive: true });
      return detectedImports;
    } else {
      return [];
    }
  });
};
const findUnusedExports = (pathToAnalyze, outputPath, onlyDeprecated) => {
  const fondueExports = [...detectedExports.deprecated, ...onlyDeprecated ? [] : detectedExports.active];
  getImports(pathToAnalyze, fondueExports).then((detectedImports) => {
    const unusedExports = fondueExports.filter(
      (selectedExport) => !detectedImports.some((detectedImport) => detectedImport.imports.includes(selectedExport))
    );
    if (outputPath) {
      writeFileSync(outputPath, JSON.stringify(unusedExports, null, 4), "utf8");
    } else {
      console.log(unusedExports);
    }
  }).catch((error) => {
    console.error("ERROR", error);
  });
};
const program = new Command();
program.name("fondue-analyzer").description("Analyze fondue usage");
program.command("find-unused-exports").description("Find unused fondue exports").argument("<path>", "Path to be analyzed").option("--only-deprecated", "Only find unused deprecated exports").option("-o, --output <path>", "Output path").action((path2, options) => {
  findUnusedExports(path2, options.output, options.onlyDeprecated);
});
program.parse();
