{ "version": 3, "sources": ["../../../Vettvangur.Heilsuvera/src/scripts/components/Sykurmagn/sortByAmount.ts"], "sourcesContent": ["interface Product {\n\tid?: string;\n\ttags?: string;\n\ttypes?: string;\n\tsugaramount: number;\n\tcaffeineamount: number;\n\tfatamount: number;\n\tsaltamount: number;\n\telement?: HTMLElement;\n}\n\nconst sortByAmount = {\n\tproducts: [] as Product[],\n\n\tinit: (): void => {\n\t\tconst selectElement = document.getElementById(\"sort-select\");\n\n\t\tsortByAmount.products = Array.from(\n\t\t\tdocument.querySelectorAll(\".product\"),\n\t\t).map((productElement) => {\n\t\t\treturn {\n\t\t\t\telement: productElement,\n\t\t\t\tcaffeineamount: parseFloat(productElement.dataset.caffeineamount),\n\t\t\t\tsugaramount: parseFloat(productElement.dataset.sugaramount),\n\t\t\t\tfatamount: parseFloat(productElement.dataset.fatamount),\n\t\t\t\tsaltamount: parseFloat(productElement.dataset.saltamount),\n\t\t\t};\n\t\t});\n\n\t\tif (selectElement) {\n\t\t\tselectElement.addEventListener(\n\t\t\t\t\"change\",\n\t\t\t\tfunction (this: HTMLSelectElement) {\n\t\t\t\t\tconst selectedValue = this.value;\n\n\t\t\t\t\tconst [field, order] = selectedValue.split(\"-\") as [\n\t\t\t\t\t\tkeyof Product,\n\t\t\t\t\t\t\"asc\" | \"desc\",\n\t\t\t\t\t];\n\n\t\t\t\t\tsortByAmount.sortProductsByField(field, order);\n\n\t\t\t\t\tsortByAmount.renderSortedProducts();\n\t\t\t\t},\n\t\t\t);\n\t\t}\n\t},\n\tsortProductsByField: (field: keyof Product, order: \"asc\" | \"desc\"): void => {\n\t\tsortByAmount.products.sort((a, b) => {\n\t\t\tconst valueA = a[field];\n\t\t\tconst valueB = b[field];\n\n\t\t\tif (typeof valueA === \"number\" && typeof valueB === \"number\") {\n\t\t\t\treturn order === \"asc\" ? valueA - valueB : valueB - valueA;\n\t\t\t} else if (typeof valueA === \"string\" && typeof valueB === \"string\") {\n\t\t\t\treturn order === \"asc\"\n\t\t\t\t\t? valueA.localeCompare(valueB)\n\t\t\t\t\t: valueB.localeCompare(valueA);\n\t\t\t} else {\n\t\t\t\treturn 0; // Fallback if the field values aren't comparable\n\t\t\t}\n\t\t});\n\t},\n\trenderSortedProducts: (): void => {\n\t\tconst productsContainer = document.querySelector(\".products \");\n\t\tif (productsContainer) {\n\t\t\tproductsContainer.innerHTML = \"\";\n\t\t\tsortByAmount.products.forEach((product) => {\n\t\t\t\tproductsContainer.appendChild(product.element);\n\t\t\t});\n\t\t}\n\t},\n};\n\nexport default sortByAmount;\n"], "mappings": "gCAWA,IAAMA,EAAe,CACpB,SAAU,CAAC,EAEX,KAAM,IAAY,CACjB,IAAMC,EAAgB,SAAS,eAAe,aAAa,EAE3DD,EAAa,SAAW,MAAM,KAC7B,SAAS,iBAA8B,UAAU,CAClD,EAAE,IAAKE,IACC,CACN,QAASA,EACT,eAAgB,WAAWA,EAAe,QAAQ,cAAc,EAChE,YAAa,WAAWA,EAAe,QAAQ,WAAW,EAC1D,UAAW,WAAWA,EAAe,QAAQ,SAAS,EACtD,WAAY,WAAWA,EAAe,QAAQ,UAAU,CACzD,EACA,EAEGD,GACHA,EAAc,iBACb,SACA,UAAmC,CAClC,IAAME,EAAgB,KAAK,MAErB,CAACC,EAAOC,CAAK,EAAIF,EAAc,MAAM,GAAG,EAK9CH,EAAa,oBAAoBI,EAAOC,CAAK,EAE7CL,EAAa,qBAAqB,CACnC,CACD,CAEF,EACA,oBAAqB,CAACI,EAAsBC,IAAgC,CAC3EL,EAAa,SAAS,KAAK,CAACM,EAAGC,IAAM,CACpC,IAAMC,EAASF,EAAEF,CAAK,EAChBK,EAASF,EAAEH,CAAK,EAEtB,OAAI,OAAOI,GAAW,UAAY,OAAOC,GAAW,SAC5CJ,IAAU,MAAQG,EAASC,EAASA,EAASD,EAC1C,OAAOA,GAAW,UAAY,OAAOC,GAAW,SACnDJ,IAAU,MACdG,EAAO,cAAcC,CAAM,EAC3BA,EAAO,cAAcD,CAAM,EAEvB,CAET,CAAC,CACF,EACA,qBAAsB,IAAY,CACjC,IAAME,EAAoB,SAAS,cAAc,YAAY,EACzDA,IACHA,EAAkB,UAAY,GAC9BV,EAAa,SAAS,QAASW,GAAY,CAC1CD,EAAkB,YAAYC,EAAQ,OAAO,CAC9C,CAAC,EAEH,CACD,EAEOC,EAAQZ", "names": ["sortByAmount", "selectElement", "productElement", "selectedValue", "field", "order", "a", "b", "valueA", "valueB", "productsContainer", "product", "sortByAmount_default"] }