// ============================================
// PATH PARSER - SVG Path zu DXF
// ============================================

function parseSvgPath(d, scaleX, flipY) {
    let entities = "";
    let currentX = 0, currentY = 0;
    let startX = 0, startY = 0;

    const commands = d.match(/[MmLlHhVvCcSsQqTtAaZz][^MmLlHhVvCcSsQqTtAaZz]*/g) || [];

    commands.forEach(cmd => {
        const type = cmd[0];
        const args = cmd.slice(1).trim().split(/[\s,]+/).filter(s => s).map(Number);

        switch(type) {
            case "M":
                currentX = args[0]; currentY = args[1];
                startX = currentX; startY = currentY;
                for(let i = 2; i < args.length; i += 2) {
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(args[i]), flipY(args[i+1]));
                    currentX = args[i]; currentY = args[i+1];
                }
                break;
            case "m":
                currentX += args[0]; currentY += args[1];
                startX = currentX; startY = currentY;
                for(let i = 2; i < args.length; i += 2) {
                    const nx = currentX + args[i], ny = currentY + args[i+1];
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(nx), flipY(ny));
                    currentX = nx; currentY = ny;
                }
                break;
            case "L":
                for(let i = 0; i < args.length; i += 2) {
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(args[i]), flipY(args[i+1]));
                    currentX = args[i]; currentY = args[i+1];
                }
                break;
            case "l":
                for(let i = 0; i < args.length; i += 2) {
                    const nx = currentX + args[i], ny = currentY + args[i+1];
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(nx), flipY(ny));
                    currentX = nx; currentY = ny;
                }
                break;
            case "H":
                for(let i = 0; i < args.length; i++) {
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(args[i]), flipY(currentY));
                    currentX = args[i];
                }
                break;
            case "h":
                for(let i = 0; i < args.length; i++) {
                    const nx = currentX + args[i];
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(nx), flipY(currentY));
                    currentX = nx;
                }
                break;
            case "V":
                for(let i = 0; i < args.length; i++) {
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(currentX), flipY(args[i]));
                    currentY = args[i];
                }
                break;
            case "v":
                for(let i = 0; i < args.length; i++) {
                    const ny = currentY + args[i];
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(currentX), flipY(ny));
                    currentY = ny;
                }
                break;
            case "Q":
            case "q":
                for(let i = 0; i < args.length; i += 4) {
                    let ex, ey;
                    if(type === "Q") {
                        ex = args[i+2]; ey = args[i+3];
                    } else {
                        ex = currentX + args[i+2]; ey = currentY + args[i+3];
                    }
                    const cx = type === "Q" ? args[i] : currentX + args[i];
                    const cy = type === "Q" ? args[i+1] : currentY + args[i+1];
                    for(let t = 0; t <= 1; t += 0.25) {
                        const t2 = Math.min(t + 0.25, 1);
                        const x1 = (1-t)*(1-t)*currentX + 2*(1-t)*t*cx + t*t*ex;
                        const y1 = (1-t)*(1-t)*currentY + 2*(1-t)*t*cy + t*t*ey;
                        const x2 = (1-t2)*(1-t2)*currentX + 2*(1-t2)*t2*cx + t2*t2*ex;
                        const y2 = (1-t2)*(1-t2)*currentY + 2*(1-t2)*t2*cy + t2*t2*ey;
                        entities += createDxfLine(scaleX(x1), flipY(y1), scaleX(x2), flipY(y2));
                    }
                    currentX = ex; currentY = ey;
                }
                break;
            case "C":
            case "c":
                for(let i = 0; i < args.length; i += 6) {
                    let c1x, c1y, c2x, c2y, ex, ey;
                    if(type === "C") {
                        c1x = args[i]; c1y = args[i+1];
                        c2x = args[i+2]; c2y = args[i+3];
                        ex = args[i+4]; ey = args[i+5];
                    } else {
                        c1x = currentX + args[i]; c1y = currentY + args[i+1];
                        c2x = currentX + args[i+2]; c2y = currentY + args[i+3];
                        ex = currentX + args[i+4]; ey = currentY + args[i+5];
                    }
                    for(let t = 0; t <= 1; t += 0.2) {
                        const t2 = Math.min(t + 0.2, 1);
                        const x1 = Math.pow(1-t,3)*currentX + 3*Math.pow(1-t,2)*t*c1x + 3*(1-t)*t*t*c2x + t*t*t*ex;
                        const y1 = Math.pow(1-t,3)*currentY + 3*Math.pow(1-t,2)*t*c1y + 3*(1-t)*t*t*c2y + t*t*t*ey;
                        const x2 = Math.pow(1-t2,3)*currentX + 3*Math.pow(1-t2,2)*t2*c1x + 3*(1-t2)*t2*t2*c2x + t2*t2*t2*ex;
                        const y2 = Math.pow(1-t2,3)*currentY + 3*Math.pow(1-t2,2)*t2*c1y + 3*(1-t2)*t2*t2*c2y + t2*t2*t2*ey;
                        entities += createDxfLine(scaleX(x1), flipY(y1), scaleX(x2), flipY(y2));
                    }
                    currentX = ex; currentY = ey;
                }
                break;
            case "Z":
            case "z":
                if(currentX !== startX || currentY !== startY) {
                    entities += createDxfLine(scaleX(currentX), flipY(currentY), scaleX(startX), flipY(startY));
                }
                currentX = startX; currentY = startY;
                break;
        }
    });

    return entities;
}
