import { message as antdMessage } from "antd";
import { uniqBy } from "lodash-es";

/**
 * 检查数组中是否存在重复项
 */
export default function useIsExistenceDuplicateSelection() {
  const isExistenceDuplicateSelection = (options) => {
    const { data, key, message = "存在重复项，请勿重复选择" } = options;
    return new Promise((resolve) => {
      if (uniqBy(data, key).length !== data.length) {
        antdMessage.error(message);
      }
      else {
        resolve();
      }
    });
  };
  return { isExistenceDuplicateSelection };
}
