import { freightClass } from "../../../../../globalUtils/freightClass.js";
import { escapeXml } from "../../../utils/xmlCharFixes.js";

export const loadItems = async (data: any) => {
    let itemGroups = "";

    for (let i = 0; i < data.length; i++) {
        let newItem = `
            <ItemGroup id="" isShipUnit="false" isHandlingUnit="false" sequence="${
                i + 1
            }">
                <ContainedBy id=""/>
                <LineItem lineNumber="${i + 1}"/>
                <Dimensions>
                    <Dimension type="Length" uom="IN">${(
                        data[i].pkgLengh / 25.4
                    ).toFixed(2)}</Dimension>
                    <Dimension type="Width" uom="IN">${(
                        data[i].pkgWidth / 25.4
                    ).toFixed(2)}</Dimension>
                    <Dimension type="Height" uom="IN">${Math.round(
                        data[i].pkgHeight / 25.4
                    ).toFixed(2)}</Dimension>
                </Dimensions>
                <Description>${`av ${data[i].article} ${escapeXml(
                    data[i].articleAlias
                )}`}</Description>
                <FreightClasses>
                    <FreightClass type="">${freightClass(
                        data[i].pkgWeight,
                        data[i].pkgLengh,
                        data[i].pkgWidth,
                        data[i].pkgHeight
                    )}</FreightClass>
                </FreightClasses>
                <Commodity/>
                <NmfcCode/>
                <HazardousMaterial>false</HazardousMaterial>
                <HazMatDetail/>
                <Weights>
                    <Weight type="actual" uom="KG">${
                        data[i].pkgWeight * data[i].Pallets
                    }</Weight>
                </Weights>
                <Quantities>
                    <Quantity type="actual" uom="pallet">${
                        data[i].Pallets
                    }</Quantity>
                </Quantities>
            </ItemGroup>
            `;

        itemGroups += newItem;
    }

    return itemGroups;
};
