/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Product
* Free version
* Unminify code for Dev
**/
var pdappAPI = {
    layer: {
        afterUpdateArea: function (e, t, o, a) {
            let i = jQuery("#pdapp-design-box").attr("data-zoom"),
                p = JSON.parse(JSON.stringify(t)),
                n = e[0].getBoundingClientRect(),
                r = n.width,
                l = n.height,
                s = e.position(),
                d = s.left,
                c = s.top,
                g = e.attr("data-target");
            if (-1 != g.indexOf("lay-")) {
                let e = g.split("lay-")[1];
                if (0 == e) {
                    let t = r / i / pdappAPI.tool.toPx(p.layers[e].width, o);
                    if (Math.round(1e3 * t) / 1e3 != 1) {
                        (p.canvas.width = parseFloat((p.canvas.width / t).toFixed(3))),
                            (p.canvas.height = parseFloat((p.canvas.height / t).toFixed(3)));
                        for (let e = 0; e < p.backgrounds.length; e++)
                            (p.backgrounds[e].width = parseFloat((p.backgrounds[e].width / t).toFixed(3))),
                                (p.backgrounds[e].height = parseFloat((p.backgrounds[e].height / t).toFixed(3))),
                                (p.backgrounds[e].left = parseFloat((p.backgrounds[e].left / t).toFixed(3))),
                                (p.backgrounds[e].top = parseFloat((p.backgrounds[e].top / t).toFixed(3)));
                        jQuery(".pdapp-viewport.active .pdapp-target.pdapp-area-layer").each(function (e) {
                            let a = jQuery(this)[0].getBoundingClientRect(),
                                n =
                                    pdappAPI.tool.toPx(p.layers[e].left, o) +
                                    (pdappAPI.tool.toPx(p.layers[e].width, o) - a.width / i) / 2,
                                r =
                                    pdappAPI.tool.toPx(p.layers[e].top, o) +
                                    (pdappAPI.tool.toPx(p.layers[e].height, o) - a.height / i) / 2;
                            (n = n / t < 0 ? 0 : n / t),
                                (r = r / t < 0 ? 0 : r / t),
                                n + pdappAPI.tool.toPx(p.layers[e].width, o) > pdappAPI.tool.toPx(p.canvas.width, o) &&
                                    (n =
                                        pdappAPI.tool.toPx(p.canvas.width, o) -
                                        pdappAPI.tool.toPx(p.layers[e].width, o)),
                                r + pdappAPI.tool.toPx(p.layers[e].height, o) >
                                    pdappAPI.tool.toPx(p.canvas.height, o) &&
                                    (r =
                                        pdappAPI.tool.toPx(p.canvas.height, o) -
                                        pdappAPI.tool.toPx(p.layers[e].height, o)),
                                (p.layers[e].left = n < 0 ? 0 : pdappAPI.layer.convertToVal(n, o)),
                                (p.layers[e].top = r < 0 ? 0 : pdappAPI.layer.convertToVal(r, o));
                        }),
                            void 0 !== p.mask.width &&
                                ((p.mask.width = parseFloat((p.mask.width / t).toFixed(3))),
                                (p.mask.height = parseFloat((p.mask.height / t).toFixed(3))),
                                (p.mask.left = parseFloat((p.mask.left / t).toFixed(3))),
                                (p.mask.top = parseFloat((p.mask.top / t).toFixed(3))));
                    } else {
                        let t = pdappAPI.layer.convertToVal(d / i, o);
                        p.layers[e].left = t < 0 ? 0 : t;
                        let a = pdappAPI.layer.convertToVal(c / i, o);
                        p.layers[e].top = a < 0 ? 0 : a;
                    }
                } else {
                    let t = pdappAPI.layer.convertToVal(d / i, o);
                    p.layers[e].left = t < 0 ? 0 : t;
                    let a = pdappAPI.layer.convertToVal(c / i, o);
                    p.layers[e].top = a < 0 ? 0 : a;
                }
            } else if (-1 != g.indexOf("bg-")) {
                (ind = g.split("bg-")[1]),
                    (p.backgrounds[ind].width = pdappAPI.layer.convertToVal(r / i, o)),
                    (p.backgrounds[ind].height = pdappAPI.layer.convertToVal(l / i, o));
                let e = pdappAPI.layer.convertToVal(d / i, o);
                p.backgrounds[ind].left = e < 0 ? 0 : e;
                let t = pdappAPI.layer.convertToVal(c / i, o);
                p.backgrounds[ind].top = t < 0 ? 0 : t;
            } else if (void 0 !== p.mask.width) {
                (p.mask.width = pdappAPI.layer.convertToVal(r / i, o)),
                    (p.mask.height = pdappAPI.layer.convertToVal(l / i, o));
                let e = pdappAPI.layer.convertToVal(d / i, o);
                p.mask.left = e < 0 ? 0 : e;
                let t = pdappAPI.layer.convertToVal(c / i, o);
                p.mask.top = t < 0 ? 0 : t;
            }
            "function" == typeof a && a(p);
        },
        convertToVal: function (e, t) {
            return (e =
                "cm" == t
                    ? pdappAPI.tool.toCm(e, "px")
                    : "in" == t
                      ? pdappAPI.tool.toIn(e, "px")
                      : parseFloat(e.toFixed(3)));
        },
        getPos: function (e) {
            let t = e.width(),
                o = e.height(),
                a = e.css("transform").replace("matrix(", "").replace(")", "").replaceAll(" ", "").split(","),
                i = { left: 0, top: 0, width: t, height: o };
            if (6 == a.length) {
                let e = parseFloat(a[0]);
                (i.left = parseFloat((parseFloat(a[4]) + (t - t * e) / 2).toFixed(3))),
                    (i.top = parseFloat((parseFloat(a[5]) + (o - o * e) / 2).toFixed(3))),
                    i.left < 0 && (i.left = 0),
                    i.top < 0 && (i.top = 0),
                    (i.width = parseFloat((t * e).toFixed(3))),
                    (i.height = parseFloat((o * e).toFixed(3)));
            }
            return i;
        },
        changeBackground: function (e, t, o, a) {
            if (
                ("image/png" != e.type && "color" != a) ||
                ("image/png" != e.type && "image/jpg" != e.type && "image/jpeg" != e.type && "color" == a)
            )
                return !1;
            let i = document.createElement("canvas"),
                p = i.getContext("2d"),
                n = new Image();
            return (
                (n.onload = function () {
                    (i.width = n.width), (i.height = n.height), p.drawImage(n, 0, 0, n.width, n.height);
                    let e = i.toDataURL();
                    t.attr("src", e),
                        setTimeout(() => {
                            "function" == typeof o && o(e);
                        });
                }),
                (n.src = URL.createObjectURL(e)),
                !0
            );
        },
        replaceColor: function (e, t, o) {
            let a = document.createElement("canvas"),
                i = a.getContext("2d"),
                p = null,
                n = null,
                r = new Image();
            (r.onload = function () {
                if (
                    ((a.width = r.width),
                    (a.height = r.height),
                    i.drawImage(r, 0, 0, r.naturalWidth, r.naturalHeight, 0, 0, r.width, r.height),
                    (p = i.getImageData(0, 0, r.width, r.height)),
                    (n = p),
                    void 0 !== n.data && n.data.length)
                ) {
                    let e = pdappAPI.tool.hexToRgb(t, !0);
                    for (let t = 0, o = p.data.length; t < o; t += 4)
                        n.data[t + 3] > 0 && ((n.data[t] = e.r), (n.data[t + 1] = e.g), (n.data[t + 2] = e.b));
                    i.putImageData(n, 0, 0);
                }
                let e = a.toDataURL("image/png");
                "function" == typeof o && o(e);
            }),
                (r.src = e);
        },
        changeMaskLayer: function (e, t) {
            if ("image/png" != e.type) return !1;
            let o = document.createElement("canvas"),
                a = o.getContext("2d"),
                i = new Image();
            return (
                (i.onload = function () {
                    (o.width = i.width), (o.height = i.height), a.drawImage(i, 0, 0, i.width, i.height);
                    let e = o.toDataURL();
                    setTimeout(() => {
                        "function" == typeof t && t(e);
                    });
                }),
                (i.src = URL.createObjectURL(e)),
                !0
            );
        },
    },
    attr: {
        generatorAttr: function (e, t) {
            let o = this;
            o.getColors(e.colors, e.attributes.color, function (a) {
                o.getSize(e.sizes, e.attributes.size, a, function (a) {
                    o.getExtras(e.extras, a, 0, function (e) {
                        let o = { key: e.key, title: e.title, stock: 999999, price: 0 };
                        "function" == typeof t && "c-s-e" != e.key && t(o);
                    });
                });
            });
        },
        getColors: function (e, t, o) {
            if (void 0 !== t && "_" != t && void 0 !== e[t])
                if ("picker" == e[t].type) "function" == typeof o && o({ key: "c_p", title: "Color Picker" });
                else
                    for (let a = 0; a < e[t].values.length; a++)
                        "function" == typeof o && o({ key: "c_" + a, title: e[t].values[a].title });
            else "function" == typeof o && o({ key: "c", title: "" });
        },
        getSize: function (e, t, o, a) {
            if (void 0 !== t && "_" != t)
                for (let t = 0; t < e.length; t++) {
                    let i = "" != e[t] && "" != o.title ? "/" + e[t] : e[t];
                    "function" == typeof a && a({ key: o.key + "-s_" + t, title: o.title + i });
                }
            else "function" == typeof a && a({ key: o.key + "-s", title: o.title });
        },
        getExtras: function (e, t, o, a) {
            if (e.length && void 0 !== e[o]) {
                let i = t.key,
                    p = t.title;
                for (let n = 0; n < e[o].values.length; n++) {
                    let r = "" != e[o].values[n] && "" != t.title ? "/" + e[o].values[n] : e[o].values[n];
                    (t.key = i + "-e_" + o + "_" + n),
                        (t.title = p + r),
                        void 0 === e[o + 1]
                            ? "function" == typeof a && a({ key: t.key, title: t.title })
                            : this.getExtras(e, t, o + 1, a);
                }
            } else "function" == typeof a && a({ key: t.key + "-e", title: t.title });
        },
    },
    tool: {
        uID: function () {
            return Math.floor(1e3 * Math.random()) + "-" + Math.floor(1e5 * Math.random());
        },
        pdappMoveAbleRef: null,
        pdappSelecToRef: null,
        getGuideLine: function (e) {
            let t = { ver: [], hor: [] },
                o = jQuery(e);
            if (o.length) {
                let e = o[0].getBoundingClientRect();
                if (0 == e.width) return t;
                let a = 0,
                    i = 0;
                t.hor.push({ pos: a, className: "pdapp-guideline" });
                for (let o = 8; o >= 1; o--)
                    t.hor.push({ pos: a + Math.round(e.height / o), className: "pdapp-guideline" });
                t.ver.push({ pos: i, className: "pdapp-guideline" });
                for (let o = 8; o >= 1; o--)
                    t.ver.push({ pos: i + Math.round(e.width / o), className: "pdapp-guideline" });
            }
            return t;
        },
        setElementGuideLines: function () {
            let e = 0,
                t = [];
            jQuery(".pdapp-viewport.active .pdapp-target").each(function () {
                let o = jQuery(this).attr("id");
                e > 7 && (e = 0);
                let a = { element: "#" + o, className: "pdapp-line-" + e };
                t.push(a), e++;
            }),
                (pdappAPI.tool.pdappMoveAbleRef.elementGuidelines = t);
        },
        startDrag: "",
        targets: [],
        enableMoveAble: function (e, t) {
            let o = this;
            o.targets = [];
            let a = o.getGuideLine(".pdapp-viewport.active"),
                i = document.querySelector(".pdapp-viewport.active"),
                p = {
                    target: o.targets,
                    draggable: !0,
                    edgeDraggable: !1,
                    scalable: !0,
                    keepRatio: !0,
                    renderDirections: ["nw", "n", "ne", "w", "e", "sw", "s", "se"],
                    rotatable: !1,
                    snappable: !1,
                    snapContainer: ".pdapp-viewport.active",
                    maxSnapElementGuidelineDistance: 200,
                    maxSnapElementGapDistance: 200,
                    pinchable: !0,
                    pinchOutside: !0,
                    verticalGuidelines: a.ver,
                    horizontalGuidelines: a.hor,
                    elementGuidelines: [],
                    bounds: { left: 0, top: 0, right: 0, bottom: 0, position: "css" },
                };
            (o.pdappMoveAbleRef = new Moveable(i, p)),
                o.pdappMoveAbleRef.on("beforeScale", (e) => {
                    e.setFixedDirection([0, 0]);
                }),
                o.pdappMoveAbleRef.on("render", (e) => {
                    (e.target.style.transform = e.transform), o.setMoveTarget();
                }),
                (o.pdappSelecToRef = new Selecto({
                    container: document.querySelector(".pdapp-viewport.active .pdapp-selecto-ref"),
                    rootContainer: document.querySelector(".pdapp-viewport.active"),
                    dragContainer: document.getElementById("pdapp-design-box"),
                    selectableTargets: [".pdapp-viewport.active .pdapp-target"],
                    hitRate: 5,
                    selectByClick: !0,
                    selectFromInside: !1,
                    toggleContinueSelect: ["shift"],
                    ratio: 0,
                }));
            let n = jQuery(".pdapp-viewport.active .moveable-control-box");
            o.pdappSelecToRef.on("dragStart", (e) => {
                const t = o.pdappMoveAbleRef,
                    a = e.inputEvent.target;
                (o.startDrag = a.style.transform),
                    (t.isMoveableElement(a) ||
                        ("function" == typeof o.targets.some && o.targets.some((e) => e === a || e.contains(a)))) &&
                        e.stop();
            }),
                o.pdappMoveAbleRef.on("scaleEnd", (t) => {
                    "function" == typeof e && e(t);
                }),
                o.pdappMoveAbleRef.on("dragEnd", (t) => {
                    "" != o.startDrag && o.startDrag != t.target.style.transform && "function" == typeof e && e(t);
                }),
                o.pdappSelecToRef.on("selectEnd", (e) => {
                    const a = o.pdappMoveAbleRef;
                    e.isDragStart &&
                        (e.inputEvent.preventDefault(),
                        a.waitToChangeTarget().then(() => {
                            a.dragStart(e.inputEvent);
                        })),
                        (function (e) {
                            if (
                                ((o.targets = e),
                                o.targets.length > 1 && (o.targets = jQuery(o.targets[0])),
                                (o.pdappMoveAbleRef.target = o.targets),
                                0 != o.targets.length)
                            ) {
                                let e = o.getGuideLine(".pdapp-viewport.active");
                                (o.pdappMoveAbleRef.verticalGuidelines = e.ver),
                                    (o.pdappMoveAbleRef.horizontalGuidelines = e.ver);
                            }
                            "function" == typeof t && t(o.targets);
                        })(e.selected);
                }),
                n.length &&
                    0 == n.find(".moveable-movebox").length &&
                    (n.append('<div class="moveable-movebox"></div>'),
                    (o.pdappMoveAbleRef.dragTarget = n.find(".moveable-movebox")[0])),
                o.pdappMoveAbleRef.on("changeTargets", (e) => {
                    o.setMoveTarget();
                });
        },
        setMoveTarget: function () {
            setTimeout(() => {
                let e = jQuery(".pdapp-viewport.active"),
                    t = jQuery(e.find(".moveable-line.moveable-direction")[0]).css("transform"),
                    o = jQuery(e.find(".moveable-line.moveable-direction")[0]).width(),
                    a = jQuery(e.find(".moveable-line.moveable-direction")[3]).width();
                e.find(".moveable-movebox").width(o),
                    e.find(".moveable-movebox").height(a),
                    e.find(".moveable-movebox").css("transform", t);
            });
        },
        colorPicker: function (e, t, o) {
            jQuery(e).spectrum({
                flat: !0,
                showInput: !1,
                showInitial: !1,
                showButtons: !1,
                showPalette: !0,
                showSelectionPalette: !0,
                localStorageKey: "spectrum.homepage",
                maxSelectionSize: 16,
                preferredFormat: "hex",
                palette: [
                    "rgb(255, 255, 255)",
                    "rgb(238 238 238)",
                    "rgb(217, 217, 217)",
                    "rgb(0, 0, 0)",
                    "rgb(75, 79, 148)",
                    "rgb(30, 144, 255)",
                    "rgb(0, 228, 232)",
                    "rgb(179, 178, 74)",
                    "rgb(93, 221, 63)",
                    "rgb(194, 228, 75)",
                    "rgb(232, 203, 203)",
                    "rgb(255, 221, 50)",
                    "rgb(255, 139, 56)",
                    "rgb(255, 68, 78)",
                    "rgb(255, 105, 180)",
                    "rgb(215, 99, 236)",
                ],
                move: function (e) {
                    let t = e.toHexString();
                    "function" == typeof o && o(t);
                },
            }),
                setTimeout(() => {
                    jQuery(".pdapp-text-groups .pdapp-list-document").text(t.document_color),
                        jQuery(".pdapp-text-groups .pdapp-list-selection").text(t.selection_color);
                }, 1);
        },
        rgbToHex: function (e) {
            if (0 === e.indexOf("#")) return e;
            if (-1 === e.indexOf("rgb(")) return e;
            var t = (e = e.replace("rgb(", "").replace(")", "")).split(", ");
            if (3 != t.length) return e;
            function o(e) {
                var t = e.toString(16);
                return 1 == t.length ? "0" + t : t;
            }
            return "#" + o(t[0]) + o(t[1]) + o(t[2]);
        },
        hexToRgb: function (e, t = !1) {
            if (0 === e.indexOf("rgb("))
                return t ? e.replace("rgb(", "").replace(")", "").replaceAll(" ", "").split(",") : e;
            if (0 !== e.indexOf("#")) {
                if (t) {
                    return (function (e) {
                        e = colorHex.replace("#", "");
                        let t = parseInt(e, 16);
                        return { r: (t >>> 16) & 255, g: (t >>> 8) & 255, b: 255 & t };
                    })(e);
                }
                return "rgb(255, 255, 255)";
            }
            var o = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);
            return t
                ? 4 == o.length
                    ? { r: parseInt(o[1], 16), g: parseInt(o[2], 16), b: parseInt(o[3], 16) }
                    : { r: 255, g: 255, b: 255 }
                : 4 == o.length
                  ? "rgb(" + parseInt(o[1], 16) + ", " + parseInt(o[2], 16) + ", " + parseInt(o[3], 16) + ")"
                  : "rgb(255, 255, 255)";
        },
        toDate: function (e) {
            let t = new Date(e.replaceAll("-", "/")).toLocaleTimeString(),
                o = e.split(" "),
                a = t.split(" "),
                i = a[0].split(":");
            return void 0 !== a[1] ? o[0] + " " + i[0] + ":" + i[1] + a[1] : e;
        },
        toPx: function (e = 0, t = "in", o = 300) {
            let a = 0;
            return (
                (a = "in" == t ? parseFloat(e) * o : "cm" == t ? (parseFloat(e) / 2.54) * o : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
        toCm: function (e = 0, t = "in", o = 300) {
            let a = 0;
            return (
                (a = "in" == t ? 2.54 * parseFloat(e) : "px" == t ? (parseFloat(e) / o) * 2.54 : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
        toIn: function (e = 0, t = "cm", o = 300) {
            let a = 0;
            return (
                (a = "cm" == t ? parseFloat(e) / 2.54 : "px" == t ? parseFloat(e) / o : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
        setLocalVar: function (e, t) {
            localStorage.setItem(e, t);
        },
        getLocalVar: function (e, t = "") {
            var o = localStorage.getItem(e);
            return null != o ? o : t;
        },
        getData: function (e) {
            return new Promise(function (t, o) {
                let a = {
                    method: "GET",
                    url: e,
                    dataType: "json",
                    success: function (e) {
                        0 == e
                            ? o({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? t({ msg: e.msg, data: e.data }) : o({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, a) {
                        o({ msg: a, error: 1 });
                    },
                };
                jQuery.ajax(a);
            });
        },
        postData: function (e, t) {
            return new Promise(function (o, a) {
                let i = {
                    method: "POST",
                    url: e,
                    dataType: "json",
                    data: t,
                    success: function (e) {
                        0 == e
                            ? a({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? o({ msg: e.msg, data: e.data }) : a({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, o) {
                        a({ msg: o, error: 1 });
                    },
                };
                jQuery.ajax(i);
            });
        },
        uploadData: function (e, t, o) {
            return new Promise(function (a, i) {
                let p = {
                    url: e,
                    type: "POST",
                    processData: !1,
                    contentType: !1,
                    dataType: "json",
                    data: t,
                    xhr: function () {
                        let e = new window.XMLHttpRequest();
                        return (
                            e.upload.addEventListener(
                                "progress",
                                function (e) {
                                    if (e.lengthComputable) {
                                        let t = (e.loaded / e.total) * 100;
                                        "function" == typeof o && o(t);
                                    }
                                },
                                !1
                            ),
                            e
                        );
                    },
                    success: function (e) {
                        0 == e
                            ? i({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? a({ msg: e.msg, data: e.data }) : i({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, o) {
                        i({ msg: o, error: 1 });
                    },
                };
                jQuery.ajax(p);
            });
        },
        createBackground(e, t, o) {
            let a = document.createElement("canvas");
            (a.width = e), (a.height = t);
            let i = a.getContext("2d");
            return (i.fillStyle = o), i.fillRect(0, 0, e, t), a.toDataURL();
        },
        toProductPX: function (e) {
            if (void 0 !== e.unit.type)
                for (let t = 0; t < e.views.length; t++) {
                    if (
                        ((e.views[t].canvas.width = pdappAPI.tool.toPx(e.views[t].canvas.width, e.unit.type)),
                        (e.views[t].canvas.height = pdappAPI.tool.toPx(e.views[t].canvas.height, e.unit.type)),
                        e.views[t].backgrounds.length)
                    )
                        for (let o = 0; o < e.views[t].backgrounds.length; o++)
                            (e.views[t].backgrounds[o].width = pdappAPI.tool.toPx(
                                e.views[t].backgrounds[o].width,
                                e.unit.type
                            )),
                                (e.views[t].backgrounds[o].height = pdappAPI.tool.toPx(
                                    e.views[t].backgrounds[o].height,
                                    e.unit.type
                                )),
                                (e.views[t].backgrounds[o].top = pdappAPI.tool.toPx(
                                    e.views[t].backgrounds[o].top,
                                    e.unit.type
                                )),
                                (e.views[t].backgrounds[o].left = pdappAPI.tool.toPx(
                                    e.views[t].backgrounds[o].left,
                                    e.unit.type
                                ));
                    if (e.views[t].layers.length)
                        for (let o = 0; o < e.views[t].layers.length; o++)
                            (e.views[t].layers[o].width = pdappAPI.tool.toPx(e.views[t].layers[o].width, e.unit.type)),
                                (e.views[t].layers[o].height = pdappAPI.tool.toPx(
                                    e.views[t].layers[o].height,
                                    e.unit.type
                                )),
                                (e.views[t].layers[o].top = pdappAPI.tool.toPx(e.views[t].layers[o].top, e.unit.type)),
                                (e.views[t].layers[o].left = pdappAPI.tool.toPx(
                                    e.views[t].layers[o].left,
                                    e.unit.type
                                ));
                    (e.views[t].mask.width = pdappAPI.tool.toPx(e.views[t].mask.width, e.unit.type)),
                        (e.views[t].mask.height = pdappAPI.tool.toPx(e.views[t].mask.height, e.unit.type));
                }
            return e;
        },
        updatePrintSize: function (e, t) {
            let o = t.unit,
                a = e.target.value;
            for (let e = 0; e < t.views.length; e++) {
                if (t.views[e].backgrounds.length)
                    for (let i = 0; i < t.views[e].backgrounds.length; i++)
                        "px" == a
                            ? ((t.views[e].backgrounds[i].width = pdappAPI.tool.toPx(
                                  t.views[e].backgrounds[i].width,
                                  o.type
                              )),
                              (t.views[e].backgrounds[i].height = pdappAPI.tool.toPx(
                                  t.views[e].backgrounds[i].height,
                                  o.type
                              )),
                              (t.views[e].backgrounds[i].top = pdappAPI.tool.toPx(
                                  t.views[e].backgrounds[i].top,
                                  o.type
                              )),
                              (t.views[e].backgrounds[i].left = pdappAPI.tool.toPx(
                                  t.views[e].backgrounds[i].left,
                                  o.type
                              )))
                            : "cm" == a
                              ? ((t.views[e].backgrounds[i].width = pdappAPI.tool.toCm(
                                    t.views[e].backgrounds[i].width,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].height = pdappAPI.tool.toCm(
                                    t.views[e].backgrounds[i].height,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].top = pdappAPI.tool.toCm(
                                    t.views[e].backgrounds[i].top,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].left = pdappAPI.tool.toCm(
                                    t.views[e].backgrounds[i].left,
                                    o.type
                                )))
                              : ((t.views[e].backgrounds[i].width = pdappAPI.tool.toIn(
                                    t.views[e].backgrounds[i].width,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].height = pdappAPI.tool.toIn(
                                    t.views[e].backgrounds[i].height,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].top = pdappAPI.tool.toIn(
                                    t.views[e].backgrounds[i].top,
                                    o.type
                                )),
                                (t.views[e].backgrounds[i].left = pdappAPI.tool.toIn(
                                    t.views[e].backgrounds[i].left,
                                    o.type
                                )));
                if (
                    ("px" == a
                        ? ((t.views[e].canvas.width = pdappAPI.tool.toPx(t.views[e].canvas.width, o.type)),
                          (t.views[e].canvas.height = pdappAPI.tool.toPx(t.views[e].canvas.height, o.type)),
                          (t.views[e].mask.width = pdappAPI.tool.toPx(t.views[e].mask.width, o.type)),
                          (t.views[e].mask.height = pdappAPI.tool.toPx(t.views[e].mask.height, o.type)))
                        : "cm" == a
                          ? ((t.views[e].canvas.width = pdappAPI.tool.toCm(t.views[e].canvas.width, o.type)),
                            (t.views[e].canvas.height = pdappAPI.tool.toCm(t.views[e].canvas.height, o.type)),
                            (t.views[e].mask.width = pdappAPI.tool.toCm(t.views[e].mask.width, o.type)),
                            (t.views[e].mask.height = pdappAPI.tool.toCm(t.views[e].mask.height, o.type)))
                          : ((t.views[e].canvas.width = pdappAPI.tool.toIn(t.views[e].canvas.width, o.type)),
                            (t.views[e].canvas.height = pdappAPI.tool.toIn(t.views[e].canvas.height, o.type)),
                            (t.views[e].mask.width = pdappAPI.tool.toIn(t.views[e].mask.width, o.type)),
                            (t.views[e].mask.height = pdappAPI.tool.toIn(t.views[e].mask.height, o.type))),
                    t.views[e].layers.length)
                )
                    for (let i = 0; i < t.views[e].layers.length; i++)
                        "px" == a
                            ? ((t.views[e].layers[i].width = pdappAPI.tool.toPx(t.views[e].layers[i].width, o.type)),
                              (t.views[e].layers[i].height = pdappAPI.tool.toPx(t.views[e].layers[i].height, o.type)),
                              (t.views[e].layers[i].top = pdappAPI.tool.toPx(t.views[e].layers[i].top, o.type)),
                              (t.views[e].layers[i].left = pdappAPI.tool.toPx(t.views[e].layers[i].left, o.type)))
                            : "cm" == a
                              ? ((t.views[e].layers[i].width = pdappAPI.tool.toCm(t.views[e].layers[i].width, o.type)),
                                (t.views[e].layers[i].height = pdappAPI.tool.toCm(t.views[e].layers[i].height, o.type)),
                                (t.views[e].layers[i].top = pdappAPI.tool.toCm(t.views[e].layers[i].top, o.type)),
                                (t.views[e].layers[i].left = pdappAPI.tool.toCm(t.views[e].layers[i].left, o.type)))
                              : ((t.views[e].layers[i].width = pdappAPI.tool.toIn(t.views[e].layers[i].width, o.type)),
                                (t.views[e].layers[i].height = pdappAPI.tool.toIn(t.views[e].layers[i].height, o.type)),
                                (t.views[e].layers[i].top = pdappAPI.tool.toIn(t.views[e].layers[i].top, o.type)),
                                (t.views[e].layers[i].left = pdappAPI.tool.toIn(t.views[e].layers[i].left, o.type)));
            }
            return (t.unit.type = a), t;
        },
        setTransformPos: function (e, t) {
            let o = jQuery(".pdapp-viewport.active")[0].getBoundingClientRect(),
                a = e[0].getBoundingClientRect(),
                i = e.position(),
                p = [1, 0, 0, 1, i.left, i.top];
            "top" == t && (p[5] = 0),
                "right" == t && (p[4] = o.width - a.width),
                "left" == t && (p[4] = 0),
                "bot" == t && (p[5] = o.height - a.height),
                ("center" != t && "centerAll" != t) || (p[4] = parseFloat(((o.width - a.width) / 2).toFixed(3))),
                ("middle" != t && "centerAll" != t) || (p[5] = parseFloat(((o.height - a.height) / 2).toFixed(3))),
                e.width(a.width),
                e.height(a.height),
                e.css("transform", "matrix(" + p.join(", ") + ")");
        },
        setTransformMove: function (e, t) {
            let o = jQuery(".pdapp-viewport.active")[0].getBoundingClientRect(),
                a = e[0].getBoundingClientRect(),
                i = e.position(),
                p = [1, 0, 0, 1, i.left, i.top];
            "up" == t && (p[5] = parseFloat(p[5]) - 1),
                "down" == t && (p[5] = parseFloat(p[5]) + 1),
                "left" == t && (p[4] = parseFloat(p[4]) - 1),
                "right" == t && (p[4] = parseFloat(p[4]) + 1),
                p[4] + a.width > o.width && (p[4] = o.width - a.width),
                p[4] < 0 && (p[4] = 0),
                p[5] + a.height > o.height && (p[5] = o.height - a.height),
                p[5] < 0 && (p[5] = 0),
                e.width(a.width),
                e.height(a.height),
                e.css("transform", "matrix(" + p.join(", ") + ")");
        },
        top: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "top");
        },
        right: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "right");
        },
        bot: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "bot");
        },
        left: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "left");
        },
        middle: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "middle");
        },
        center: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "center");
        },
        centerAll: function (e) {
            e.length && jQuery(".pdapp-viewport.active").length && pdappAPI.tool.setTransformPos(e, "centerAll");
        },
        fitInside: function (e) {
            let t = jQuery("#pdapp-design-box .pdapp-viewport.active").width(),
                o = jQuery("#pdapp-design-box .pdapp-viewport.active").height(),
                a = e.width(),
                i = e.height(),
                p = t > o ? o : t;
            a > i ? ((i = (i / a) * p), (a = p)) : ((a = (a / i) * p), (i = p)),
                e.width(a),
                e.height(i),
                e.css("transform", "matrix(1, 0, 0, 1, 0, 0)");
        },
        updateURL(e) {
            let t = pdappAdminUrl + "admin.php?page=product-designer-app-" + e;
            window.history.pushState(null, "", t);
        },
        changeThumb(e, t, o) {
            if ("image/png" != e.type && "image/jpg" != e.type && "image/jpeg" != e.type) return !1;
            let a = document.createElement("canvas"),
                i = a.getContext("2d"),
                p = new Image();
            return (
                (p.onload = function () {
                    (a.width = void 0 === o ? 500 : o), (a.height = a.width);
                    let e = a.width,
                        n = a.width,
                        r = 0,
                        l = 0;
                    p.width > p.height
                        ? ((n = (a.width * p.height) / p.width), (r = Math.round((a.width - n) / 2)))
                        : ((e = (a.width * p.width) / p.height), (l = Math.round((a.width - e) / 2))),
                        i.drawImage(p, l, r, e, n);
                    let s = a.toDataURL();
                    setTimeout(() => {
                        "function" == typeof t && t(s);
                    });
                }),
                (p.src = URL.createObjectURL(e)),
                !0
            );
        },
        getProductLayout: function (e, t) {
            let o = {
                cartdesign: !0,
                addons: {},
                customview: !1,
                maxview: 10,
                viewtitle: "",
                viewcopy: 0,
                customize: !1,
                download: e.download.allow,
                saveDesign: e.saveDesign.allow,
                addcart: e.addcart.allow,
                quote: e.quote.allow,
                template: e.template.allow,
                text: e.text.allow,
                upload: e.photo.allowUpload,
                library: e.photo.allowLibrary,
                qrcode: e.qrcode.allow,
                barcode: e.barcode.allow,
                layer: e.layer.allow,
                team: e.team.allow,
            };
            for (let e in t) void 0 !== o[e] && (o[e] = t[e]);
            return o;
        },
    },
    hotkeys: {
        init: function (e) {
            this.move(e), this.del(e);
        },
        move: function (e) {
            hotkeys("up", function (t) {
                return "function" == typeof e && e("move", "up"), !1;
            }),
                hotkeys("down", function (t) {
                    return "function" == typeof e && e("move", "down"), !1;
                }),
                hotkeys("left", function (t) {
                    return "function" == typeof e && e("move", "left"), !1;
                }),
                hotkeys("right", function (t) {
                    return "function" == typeof e && e("move", "right"), !1;
                });
        },
        del: function (e) {
            hotkeys("delete, command+backspace", function (t) {
                let o = pdappAPI.tool.pdappMoveAbleRef.target;
                if (null != o && o.length > 0 && "function" == typeof e)
                    if (jQuery(o).hasClass("pdapp-product-img")) {
                        let t = jQuery(o).attr("data-target");
                        if (-1 != t.indexOf("bg-")) {
                            let o = t.split("bg-")[1];
                            0 != o && e("del", "bg", o);
                        }
                    } else if (jQuery(o).hasClass("mask-layer")) e("del", "mask", 0);
                    else if (jQuery(o).hasClass("pdapp-area-layer")) {
                        let t = jQuery(o).attr("data-target");
                        if (-1 != t.indexOf("lay-")) {
                            let o = t.split("lay-")[1];
                            0 != o && e("del", "layer", o);
                        }
                    }
                return !1;
            });
        },
    },
    exportData: function (e, t) {
        let o = this,
            a = 0,
            i = 0,
            p = !1;
        for (let n = 0; n < e.views.length; n++) {
            for (let r = 0; r < e.views[n].backgrounds.length; r++)
                "" != e.views[n].backgrounds[r].image &&
                    (a++,
                    o.renderImg(e.views[n].backgrounds[r].image, e.id, function (o) {
                        (e.views[n].backgrounds[r].image = o),
                            i++,
                            a != i ||
                                -1 == e.thumb.indexOf("base64,") ||
                                p ||
                                ((p = !0), "function" == typeof t && t(e));
                    }));
            for (let r = 0; r < e.views[n].layers.length; r++)
                "" != e.views[n].layers[r].mask &&
                    (a++,
                    o.renderImg(e.views[n].layers[r].mask, e.id, function (o) {
                        (e.views[n].layers[r].mask = o),
                            i++,
                            a != i ||
                                -1 == e.thumb.indexOf("base64,") ||
                                p ||
                                ((p = !0), "function" == typeof t && t(e));
                    }));
            void 0 !== e.views[n].mask.image &&
                "" != e.views[n].mask.image &&
                (a++,
                o.renderImg(e.views[n].mask.image, e.id, function (o) {
                    (e.views[n].mask.image = o),
                        i++,
                        a != i || -1 == e.thumb.indexOf("base64,") || p || ((p = !0), "function" == typeof t && t(e));
                }));
        }
        o.renderImg(e.thumb, e.id, function (o) {
            (e.thumb = o), a != i || p || ((p = !0), "function" == typeof t && t(e));
        });
    },
    renderImg: function (e, t, o) {
        if (-1 != e.indexOf("base64,") || -1 != e.indexOf("://")) return "function" == typeof o && o(e), !1;
        let a = document.createElement("canvas"),
            i = a.getContext("2d"),
            p = new Image();
        (p.onload = function () {
            (a.width = p.width), (a.height = p.height), i.drawImage(p, 0, 0, p.width, p.height);
            let e = a.toDataURL();
            setTimeout(() => {
                "function" == typeof o && o(e);
            });
        }),
            (p.src = pdappUploadUrl + "products/product-" + t + "/" + e);
    },
    encodeBase64: function (e) {
        let t = unescape(encodeURIComponent(e));
        return btoa(t);
    },
    decodeBase64: function (e) {
        return (e = e.replace(/\s/g, "")), decodeURIComponent(escape(atob(e)));
    },
    folder: {
        getFolders: function (e, t, o) {
            let a = [],
                i = void 0 !== e[0] ? { id: 0, list: e[0] } : { id: 0, list: [] };
            this.getFolder(e, i, t, function (e) {
                a.push(e), 0 == e.id && (a.reverse(), "function" == typeof o && o(a));
            });
        },
        getFolder: function (e, t, o, a) {
            if (0 != o) {
                let i = 0;
                for (let t in e)
                    for (let p = 0; p < e[t].length; p++)
                        e[t][p].id == o &&
                            ((i = e[t][p].pid), void 0 !== e[o] && "function" == typeof a && a({ id: o, list: e[o] }));
                0 == i
                    ? "function" == typeof a && a(t)
                    : this.getFolder(e, t, i, function (e) {
                          "function" == typeof a && a(e);
                      });
            } else "function" == typeof a && a(t);
        },
    },
};