import fileUploadFuctions from "@/utils/fileUploadFuctions";
const unitsFunctions = {
    /**
     * 获取新的布展单元，实装媒体id，
     * @param {*} exhibitionUnits 
     * @param {*} upLoadRes 
     * @returns 
     */
    getNewUnits(exhibitionUnits, upLoadRes) {
        const unitCopy = JSON.parse(JSON.stringify(exhibitionUnits))//不要修改原本的数据
        const callback = (arr) => {
            arr.forEach(unitItem => {
                // 每一个unitItem有自己的euid，有唯一的一个images,唯一的一个videos
                // 目的是要让每一个unitItem的images，videos实装回去,crId拼接回去
                // mediaObj形如{
                //     images: "1,2,3",
                //     videos: "4,5,6",
                //   }
                const mediaForm = unitsFunctions.getMediaForm(unitItem, upLoadRes)
                const crForm = unitsFunctions.getCrForm(unitItem)
                unitItem = Object.assign(unitItem, mediaForm, crForm)
            });
        }
        // 遍历布展单元，并执行callback
        unitsFunctions.loopUnits(unitCopy, callback)
        return unitCopy
    },

    getMediaForm(unitItem, upLoadRes) {
        const { euId } = unitItem
        const mediaForm = unitsFunctions.getUnitFileIdstrObj(euId, upLoadRes)
        return mediaForm
    },

    getCrForm(unitItem) {
        const obj = {
            crIds: ''
        }
        const { crIds } = unitItem
        if (crIds instanceof Array) {
            obj.crIds = crIds.join(",");
        } else if (crIds) {
            obj.crIds = crIds
        }
        return obj
    },
    /**
     * 
     * @param {Array} arr 布展单元的数组
     * @param {Function} callback 循环处理什么事
     */
    loopUnits(arr, callback) {
        if (arr.length > 0) {
            callback(arr)
            if (arr.children) {
                unitsFunctions.loopUnits(arr.children, callback)
            }
        }
    },
    /**
 * 获取布展单元下面的媒体的id字符串
 * @param {String} euId 布展单元的key euId
 * @param {String} res 上传后的结果 如unit-images-${euid}
 * @returns {Obj} euId对应媒体文件ID字符串
 * 形如{
    images: "1,2,3",
    videos: "4,5,6",
}item.fileId
*/
    getUnitFileIdstrObj(euId, res) {
        const obj = {};
        const unitMediaKeys = ["images"];
        const unitMediaFileIdArr = unitMediaKeys.map((mediaKey) => {
            const filterArr = res.data.filter((item) => {
                // 如果是当前的euId下的文件
                const str1 = `unit-${mediaKey}-${euId}`
                const arr = item.fileKey.split('-')
                const str2 = arr.slice(0, 3).join('-') //前三位拼接
                return str1 === str2
            });
            const arr = filterArr.map(item => {
                return item.fileId
            })
            return arr;
        });
        unitMediaKeys.forEach((key, index) => {
            if (unitMediaFileIdArr[index].length > 0) {
                obj[key] = unitMediaFileIdArr[index].join(",");
            } else {
                obj[key] = "";
            }
        });
        return obj;
    },

    /**
 * 添加布展单元的图片至formData
 * @param {array} unitData 布展单元数据
 * @param {FormData} formData 需要上传的表单
 */
    addUnitImageToFormData(unitData, formData) {
        const callback = (arr) => {
            arr.forEach(unitItem => {
                unitsFunctions.appendMediaItemToFormData(unitItem, formData, "images");
            })
        }
        unitsFunctions.loopUnits(unitData, callback)
    },
    /**
     * 添加布展单元媒体item至formData
     * @param {array} unitItem 布展单元的item
     * @param {FormData} media 表单中存储ids的key
     * @param {FormData} mediaVo 表单中存储文件List的key
     */
    appendMediaItemToFormData(unitItem, formData, mediaKey) {
        const mediaVo = unitItem[`${mediaKey}Vo`];
        if (mediaVo && mediaVo.length > 0) {
            mediaVo.forEach((file, index) => {
                if (fileUploadFuctions.isFileRaw(file)) {
                    const uploadKey = `unit-${mediaKey}-${unitItem.euId}-${index}`;
                    formData.append(uploadKey, file.raw);
                }
            });
        }
    },

    getUnitDeleteArr(newUnit, oldUnit) {
        const deleteArr = []

        return deleteArr
    }
}

export default unitsFunctions