import router from "../router/index.js";
import {useSystemStore} from "../pinia/SystemStore/index.js";
import {Message} from "@arco-design/web-vue";

export const toPath = (path, query = {}, flag = false) => {
    router.push({
        path: path, query: query
    }).then(() => {
        const SystemStore = useSystemStore();
        if (!flag) {
            SystemStore.NOW_ROUTER = path;
            Object.assign(SystemStore.NOW_ROUTER_QUERY, query);
        }
    });
}

export const openPage = (path, query = {}) => {
    window.open(`${import.meta.env.VITE_WEB_HOST}/#/manage-materials`, '_blank');
}

export const VITE_TINYMCE_KEY = () => {
    return import.meta.env.VITE_TINYMCE_KEY;
}

export const deleteObjectFields = (obj) => {
    Object.keys(obj).forEach(key => {
        delete obj[key];
    });
}

export const baseImage = (url) => {
    if (!url) url = '';
    return url.startsWith('http') ? url : import.meta.env.VITE_API_URL + url;
}

export const openUrl = (url) => {
    window.open(url);
}

export const determineMediaType = (url) => {
    const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'];
    const videoExtensions = ['.mp4', '.mov', '.avi', '.mkv', '.webm'];

    const extension = url.substring(url.lastIndexOf('.')).toLowerCase();

    if (imageExtensions.includes(extension)) {
        return 'Image';
    } else if (videoExtensions.includes(extension)) {
        return 'Video';
    } else {
        return 'Unknown';
    }
}

export const findSwappedIds = (original, swapped) => {
    let id1, id2;

    original.forEach((item, index) => {
        if (item.id !== swapped[index].id) {
            if (!id1) {
                id1 = item.id; // 记录第一个不同的 id
            } else {
                id2 = item.id; // 记录第二个不同的 id
            }
        }
    });

    return [id1, id2];
};

export const copy = (text) => {
    navigator.clipboard.writeText(text).then(() => {
        Message.success('字符串已复制到剪贴板');
    }).catch(err => {
        Message.warning('复制失败');
    });
}
