import { f as fail } from './index-B2LGyy1l.js'; const COLLECTION = "Stackq_Items"; function parseNum(val) { if (val === "" || val === null || val === void 0) return void 0; const n = Number(val); return Number.isFinite(n) ? n : void 0; } function parseBool(val) { return val === "on" || val === "true" || val === true; } const load = async ({ locals }) => { const list = await locals.pb.collection(COLLECTION).getFullList({ sort: "-created", expand: "Parent" }).catch((err) => { console.error(COLLECTION, err); return []; }); const itemsForParent = list.map((i) => ({ id: i.id, Item: i.Item, SKU: i.SKU })); return { items: list, itemsForParent }; }; const actions = { create: async ({ request, locals }) => { const form = await request.formData(); const Item = form.get("Item")?.trim() ?? ""; const SKU = form.get("SKU")?.trim() ?? ""; const Description = form.get("Description")?.trim() ?? ""; const UOM = form.get("UOM")?.trim() ?? ""; const Cost = parseNum(form.get("Cost")); const Vendor = form.get("Vendor")?.trim() ?? ""; const Catagory = form.get("Catagory")?.trim() ?? ""; const Sub_Catagory = form.get("Sub_Catagory")?.trim() ?? ""; const Has_Parent = parseBool(form.get("Has_Parent")); const Parent = form.get("Parent")?.trim() ?? ""; const Stock = parseBool(form.get("Stock")); const imageFileCreate = form.get("Image"); const hasImage = imageFileCreate && imageFileCreate.size > 0; if (!Item) return fail(400, { error: "Item name is required", form: "create" }); try { const body = { Item, SKU, Description, UOM: UOM || void 0, Cost: Cost ?? 0, Vendor, Catagory: Catagory || void 0, Sub_Catagory: Sub_Catagory || void 0, Has_Parent: !!Has_Parent, Parent: Parent || null, Stock: !!Stock }; if (hasImage) body.Image = imageFileCreate; await locals.pb.collection(COLLECTION).create(body); } catch (e) { const message = e && typeof e === "object" && "message" in e ? String(e.message) : "Create failed"; return fail(500, { error: message, form: "create" }); } return { success: true }; }, update: async ({ request, locals }) => { const form = await request.formData(); const id = form.get("id")?.trim(); if (!id) return fail(400, { error: "Missing id", form: "update" }); const Item = form.get("Item")?.trim() ?? ""; const SKU = form.get("SKU")?.trim() ?? ""; const Description = form.get("Description")?.trim() ?? ""; const UOM = form.get("UOM")?.trim() ?? ""; const Cost = parseNum(form.get("Cost")); const Vendor = form.get("Vendor")?.trim() ?? ""; const Catagory = form.get("Catagory")?.trim() ?? ""; const Sub_Catagory = form.get("Sub_Catagory")?.trim() ?? ""; const Has_Parent = parseBool(form.get("Has_Parent")); const Parent = form.get("Parent")?.trim() ?? ""; const Stock = parseBool(form.get("Stock")); const Image = form.get("Image"); const imageFile = Image && Image.size > 0 ? Image : void 0; if (!Item) return fail(400, { error: "Item name is required", form: "update" }); try { const body = { Item, SKU, Description, UOM: UOM || void 0, Cost: Cost ?? 0, Vendor, Catagory: Catagory || void 0, Sub_Catagory: Sub_Catagory || void 0, Has_Parent: !!Has_Parent, Parent: Parent || null, Stock: !!Stock }; if (imageFile) body.Image = imageFile; await locals.pb.collection(COLLECTION).update(id, body); } catch (e) { const message = e && typeof e === "object" && "message" in e ? String(e.message) : "Update failed"; return fail(500, { error: message, form: "update" }); } return { success: true }; }, delete: async ({ request, locals }) => { const form = await request.formData(); const id = form.get("id")?.trim(); if (!id) return fail(400, { error: "Missing id", form: "delete" }); try { await locals.pb.collection(COLLECTION).delete(id); } catch (e) { const message = e && typeof e === "object" && "message" in e ? String(e.message) : "Delete failed"; return fail(500, { error: message, form: "delete" }); } return { success: true }; } }; var _page_server_ts = /*#__PURE__*/Object.freeze({ __proto__: null, actions: actions, load: load }); const index = 13; let component_cache; const component = async () => component_cache ??= (await import('./_page.svelte-B3A5I2KK.js')).default; const server_id = "src/routes/items/+page.server.ts"; const imports = ["_app/immutable/nodes/13.D3zuOlaF.js","_app/immutable/chunks/Cel1DXuQ.js","_app/immutable/chunks/B5gxx3nr.js","_app/immutable/chunks/DScGFiBY.js","_app/immutable/chunks/BlbL38UD.js","_app/immutable/chunks/Bzak7iHL.js","_app/immutable/chunks/Cua5CQwk.js","_app/immutable/chunks/ChlQth4G.js","_app/immutable/chunks/DoRZqEmo.js","_app/immutable/chunks/BfWekp-4.js","_app/immutable/chunks/BuAKAdPx.js","_app/immutable/chunks/DzJWa70M.js","_app/immutable/chunks/B7h8GtFa.js","_app/immutable/chunks/BNQ00IGn.js","_app/immutable/chunks/BPc-JNDc.js","_app/immutable/chunks/fb4NW7RP.js","_app/immutable/chunks/DkOFPg3x.js"]; const stylesheets = []; const fonts = []; export { component, fonts, imports, index, _page_server_ts as server, server_id, stylesheets }; //# sourceMappingURL=13-B4VrEywB.js.map