import type { ICopyText } from './types';

/**
 * 复制到剪切板
 * @param text 待复制的文本
 * @param showToast 复制成功是否弹提示，默认 true 弹"内容已复制"
 */
const copyText: ICopyText = function (text: string, showToast = true) {
  return new Promise((resolve, reject) => {
    if (uni) {
      uni.setClipboardData({
        showToast,
        data: text as string,
        success: () => {
          resolve();
        },
        fail: (error: Error) => {
          console.log(error);
          reject();
        },
      });
    } else {
      reject();
    }
  });
};

export { copyText };
