import { defineStore } from 'pinia'; import { ref } from 'vue'; import { a as useConfigStore, aw as uploadFile, e as api$1, ax as updateProduct, ay as createProduct } from './server.mjs'; import { f as files, a as firstFiles } from './handle-Bvvl_htN.mjs'; import { cloneDeep, differenceWith, isEqual, differenceBy } from 'lodash-es'; const useProductStore = defineStore("product", () => { const originalFile = ref(); const mediaFiles = ref([]); const percentUpload = ref(0); const loading = ref(false); const isFileChanged = ref(false); const config = useConfigStore(); let progress = 0; const fileUpLoader = async (formFields, id = -1) => { loading.value = true; progress = cloneDeep(100 / mediaFiles.value.length + 3); percentUpload.value = progress; if (id != -1) { await update(formFields, id); } else { await create(formFields); } }; const create = async (formFields) => { var _a, _b, _c; let tempFormFields = cloneDeep(formFields); const originalUrl = await uploadFile(originalFile.value.file); percentUpload.value = progress; try { const uploadPromises = mediaFiles.value.map(async (item, index) => { var _a2; const mediaUrl = await uploadFile(item.file); percentUpload.value = progress * (index + 1); return { url: (_a2 = mediaUrl.data) == null ? void 0 : _a2.fullPath, type: item.file.type.includes("video") ? "video" : "image" }; }); tempFormFields.original_image = (_a = originalUrl.data) == null ? void 0 : _a.fullPath; tempFormFields.files.push({ url: (_b = originalUrl.data) == null ? void 0 : _b.fullPath, type: originalFile.value.file.type.includes("video") ? "video" : "image" }); const uploadedFiles = await Promise.all(uploadPromises); tempFormFields.files.push(...uploadedFiles); tempFormFields.price = tempFormFields.price ? parseInt(tempFormFields.price) : 0; tempFormFields.weight = tempFormFields.weight ? parseFloat(tempFormFields.weight) : 0; tempFormFields.percent_fee = tempFormFields.percent_fee ? parseFloat(tempFormFields.percent_fee) : 0; tempFormFields.rial_fee = tempFormFields.rial_fee ? parseInt(tempFormFields.rial_fee) : 0; tempFormFields.count = tempFormFields.count ? parseInt(tempFormFields.count) : void 0; tempFormFields.discount = tempFormFields.discount ? parseInt(tempFormFields.discount) : void 0; tempFormFields.box_size = parseInt(tempFormFields.box_size); (_c = tempFormFields.variants) == null ? void 0 : _c.forEach((val) => { val.discount = val.discount ? parseInt(val.discount) : void 0; }); } catch (e) { api$1.error(config.useString("theProductImageDidNotLoadCorrectly")); percentUpload.value = 0; console.log(e); throw new Error(config.useString("theProductImageDidNotLoadCorrectly")); } console.log(tempFormFields, originalUrl.error); if (tempFormFields.original_image && !originalUrl.error) { try { await createProduct(tempFormFields); percentUpload.value = 100; api$1.success(config.useString("addedSuccessfully")); } catch (e) { console.log(e); api$1.error(e); percentUpload.value = 0; throw new Error(e); } finally { loading.value = false; } } else { api$1.error(config.useString("theProductImageDidNotLoadCorrectly")); loading.value = false; throw new Error(config.useString("theProductImageDidNotLoadCorrectly")); } }; const update = async (formFields, id) => { var _a; let indexs; let tempFormFields = cloneDeep(formFields); if (files.value.length >= firstFiles.value.length) indexs = differenceWith(files.value, firstFiles.value, isEqual); else { let temp = differenceBy(firstFiles.value, files.value, "index"); let imagesDeleted = []; temp.forEach((val) => { if (!files.value.some((x) => { return x.index == val.index; })) imagesDeleted.push(val); }); imagesDeleted.forEach((x) => { firstFiles.value.splice(x.index, 1); tempFormFields.files.splice(x.index, 1, null); }); indexs = await differenceWith(files.value, firstFiles.value, isEqual); } let uploadedFiles; if (indexs.length != 0) { if (originalFile.value) mediaFiles.value.splice(0, 0, originalFile.value); const uploadPromises = mediaFiles.value.map(async (item, index) => { var _a2, _b; const mediaUrl = await uploadFile(item.file); percentUpload.value = progress * (index + 1); if (mediaUrl.error) { api$1.error(config.useString("theProductImageDidNotLoadCorrectly")); throw new Error(config.useString("theProductImageDidNotLoadCorrectly")); } return { value: { url: (_a2 = mediaUrl.data) == null ? void 0 : _a2.fullPath, type: item.file.type.includes("video") ? "video" : "image" }, id: (_b = indexs[index]) == null ? void 0 : _b.index }; }); uploadedFiles = await Promise.all(uploadPromises); if (!tempFormFields.files) tempFormFields.files = []; uploadedFiles.forEach((x, i) => { tempFormFields.files[x.id] = x.value; }); } tempFormFields.files = tempFormFields.files.filter((value) => value != null); tempFormFields.original_image = tempFormFields.files[0].url; tempFormFields.price = tempFormFields.price ? parseInt(tempFormFields.price) : void 0; tempFormFields.weight = tempFormFields.weight ? parseFloat(tempFormFields.weight) : void 0; tempFormFields.percent_fee = tempFormFields.weight ? parseFloat(tempFormFields.percent_fee) : void 0; tempFormFields.rial_fee = tempFormFields.rial_fee ? parseInt(tempFormFields.rial_fee) : 0; tempFormFields.count = tempFormFields.count ? parseInt(tempFormFields.count) : void 0; tempFormFields.discount = tempFormFields.discount ? parseInt(tempFormFields.discount) : void 0; (_a = tempFormFields.variants) == null ? void 0 : _a.forEach((val) => { val.discount = val.discount ? parseInt(val.discount) : void 0; }); tempFormFields.box_size = parseInt(tempFormFields.box_size); if (tempFormFields.discount == null) tempFormFields.discount = void 0; console.log(tempFormFields, uploadedFiles, mediaFiles, indexs); try { percentUpload.value = 99.5; await updateProduct(tempFormFields, id); percentUpload.value = 100; api$1.success(config.useString("updatedSuccessfully")); } catch (e) { console.log(e); percentUpload.value = 0; api$1.error(e); throw new Error(e); } finally { loading.value = false; } }; return { originalFile, percentUpload, mediaFiles, fileUpLoader, isFileChanged, loading }; }); export { useProductStore as u }; //# sourceMappingURL=product-DaDj7A6D.mjs.map