import {
  createPlugins,
  PlatePlugin,
  PlatePluginComponent,
} from '@udecode/plate-common';

import { useCreatePlugins } from './use-create-plugin';
import { useCreatePluginsUI } from './use-create-plugins-ui';

export interface PluginOptions {
  plugins?: PlatePlugin[];
  draggable?: boolean;
  placeholder?: boolean;
  excludePluginKeys?: string[];
  overrideByKey?: Partial<Record<string, PlatePluginComponent>>;
}

export const useCreateFlowesPlugin = ({
  plugins = [],
  draggable,
  placeholder,
  overrideByKey,
  excludePluginKeys = [],
}: PluginOptions) => {
  const createdPlugins = useCreatePlugins(plugins);
  const createdComponents = useCreatePluginsUI(overrideByKey, {
    placeholder,
    draggable,
  });

  const _plugins = createPlugins(createdPlugins, {
    components: createdComponents,
  });

  return _plugins.filter((item) => !excludePluginKeys.includes(item.key));
};
