'use strict';

/**
 * Gradient types and utilities for VectorEditor
 */
/* ────────────────────────────────────────────────────────────────
   GRADIENT ID GENERATOR
──────────────────────────────────────────────────────────────── */
let _gradId = 0;
const gradientId = () => `grad${++_gradId}`;
/* ────────────────────────────────────────────────────────────────
   PRESET GRADIENTS
──────────────────────────────────────────────────────────────── */
const PRESET_GRADIENTS = [
    // Linear gradients
    {
        type: "linear",
        id: gradientId(),
        angle: 0,
        stops: [
            { offset: 0, color: "#ff0000" },
            { offset: 1, color: "#0000ff" },
        ],
    },
    {
        type: "linear",
        id: gradientId(),
        angle: 45,
        stops: [
            { offset: 0, color: "#00ff00" },
            { offset: 0.5, color: "#ffff00" },
            { offset: 1, color: "#ff0000" },
        ],
    },
    {
        type: "linear",
        id: gradientId(),
        angle: 90,
        stops: [
            { offset: 0, color: "#ffffff" },
            { offset: 1, color: "#000000" },
        ],
    },
    {
        type: "linear",
        id: gradientId(),
        angle: 0,
        stops: [
            { offset: 0, color: "#667eea" },
            { offset: 1, color: "#764ba2" },
        ],
    },
    {
        type: "linear",
        id: gradientId(),
        angle: 45,
        stops: [
            { offset: 0, color: "#f093fb" },
            { offset: 1, color: "#f5576c" },
        ],
    },
    // Radial gradients
    {
        type: "radial",
        id: gradientId(),
        cx: 0.5,
        cy: 0.5,
        r: 0.7,
        stops: [
            { offset: 0, color: "#ffffff" },
            { offset: 1, color: "#000000" },
        ],
    },
    {
        type: "radial",
        id: gradientId(),
        cx: 0.3,
        cy: 0.3,
        r: 0.8,
        stops: [
            { offset: 0, color: "#ffff88" },
            { offset: 0.5, color: "#ff8800" },
            { offset: 1, color: "#880000" },
        ],
    },
    {
        type: "radial",
        id: gradientId(),
        cx: 0.5,
        cy: 0.5,
        r: 0.5,
        stops: [
            { offset: 0, color: "#00ffff", opacity: 1 },
            { offset: 1, color: "#00ffff", opacity: 0 },
        ],
    },
];
/* ────────────────────────────────────────────────────────────────
   GRADIENT TO SVG CONVERSION
──────────────────────────────────────────────────────────────── */
function gradientToSVGDef(gradient) {
    const stopsSVG = gradient.stops
        .map((stop) => `<stop offset="${stop.offset * 100}%" stop-color="${stop.color}"${stop.opacity !== undefined ? ` stop-opacity="${stop.opacity}"` : ""}/>`)
        .join("\n      ");
    switch (gradient.type) {
        case "linear": {
            const rad = (gradient.angle * Math.PI) / 180;
            const x1 = 50 - Math.cos(rad) * 50;
            const y1 = 50 - Math.sin(rad) * 50;
            const x2 = 50 + Math.cos(rad) * 50;
            const y2 = 50 + Math.sin(rad) * 50;
            return `<linearGradient id="${gradient.id}" x1="${x1}%" y1="${y1}%" x2="${x2}%" y2="${y2}%">
      ${stopsSVG}
    </linearGradient>`;
        }
        case "radial": {
            const fxAttr = gradient.fx !== undefined ? ` fx="${gradient.fx * 100}%"` : "";
            const fyAttr = gradient.fy !== undefined ? ` fy="${gradient.fy * 100}%"` : "";
            return `<radialGradient id="${gradient.id}" cx="${gradient.cx * 100}%" cy="${gradient.cy * 100}%" r="${gradient.r * 100}%"${fxAttr}${fyAttr}>
      ${stopsSVG}
    </radialGradient>`;
        }
        case "conic": {
            // SVG doesn't support conic gradients natively, so we approximate with many linear stops
            // This creates a radial gradient with the same appearance
            return `<radialGradient id="${gradient.id}" cx="${gradient.cx * 100}%" cy="${gradient.cy * 100}%" r="50%">
      ${stopsSVG}
    </radialGradient>`;
        }
    }
}
/* ────────────────────────────────────────────────────────────────
   GRADIENT TO CSS
──────────────────────────────────────────────────────────────── */
function gradientToCSS(gradient) {
    const stops = gradient.stops
        .map((stop) => {
        const opacity = stop.opacity !== undefined ? stop.opacity : 1;
        const color = hexToRGBA(stop.color, opacity);
        return `${color} ${stop.offset * 100}%`;
    })
        .join(", ");
    switch (gradient.type) {
        case "linear":
            return `linear-gradient(${gradient.angle}deg, ${stops})`;
        case "radial":
            return `radial-gradient(ellipse ${gradient.r * 100}% ${gradient.r * 100}% at ${gradient.cx * 100}% ${gradient.cy * 100}%, ${stops})`;
        case "conic":
            return `conic-gradient(from ${gradient.angle}deg at ${gradient.cx * 100}% ${gradient.cy * 100}%, ${stops})`;
    }
}
function hexToRGBA(hex, opacity) {
    const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    if (!result)
        return hex;
    const r = parseInt(result[1], 16);
    const g = parseInt(result[2], 16);
    const b = parseInt(result[3], 16);
    return `rgba(${r}, ${g}, ${b}, ${opacity})`;
}
/* ────────────────────────────────────────────────────────────────
   GRADIENT MANIPULATION
──────────────────────────────────────────────────────────────── */
function cloneGradient(gradient) {
    return JSON.parse(JSON.stringify(gradient));
}
function createLinearGradient(colors, angle = 0) {
    const stops = colors.map((color, i) => ({
        offset: i / (colors.length - 1),
        color,
    }));
    return {
        type: "linear",
        id: gradientId(),
        angle,
        stops,
    };
}
function createRadialGradient(colors, cx = 0.5, cy = 0.5, r = 0.7) {
    const stops = colors.map((color, i) => ({
        offset: i / (colors.length - 1),
        color,
    }));
    return {
        type: "radial",
        id: gradientId(),
        cx,
        cy,
        r,
        stops,
    };
}

exports.PRESET_GRADIENTS = PRESET_GRADIENTS;
exports.cloneGradient = cloneGradient;
exports.createLinearGradient = createLinearGradient;
exports.createRadialGradient = createRadialGradient;
exports.gradientId = gradientId;
exports.gradientToCSS = gradientToCSS;
exports.gradientToSVGDef = gradientToSVGDef;
//# sourceMappingURL=vectorGradients.js.map
