/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Order
* Free version
* Unminify code for Dev
**/
var pdappAPI = {
    tool: {
        toDate: function (e) {
            let t = new Date(e.replaceAll("-", "/")).toLocaleTimeString(),
                n = e.split(" "),
                a = t.split(" "),
                r = a[0].split(":");
            return void 0 !== a[1] ? n[0] + " " + r[0] + ":" + r[1] + a[1] : e;
        },
        getData: function (e) {
            return new Promise(function (t, n) {
                let a = {
                    method: "GET",
                    url: e,
                    dataType: "json",
                    success: function (e) {
                        0 == e
                            ? n({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? t({ msg: e.msg, data: e.data }) : n({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, a) {
                        n({ msg: a, error: 1 });
                    },
                };
                jQuery.ajax(a);
            });
        },
        postData: function (e, t) {
            return new Promise(function (n, a) {
                let r = {
                    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 ? n({ msg: e.msg, data: e.data }) : a({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, n) {
                        a({ msg: n, error: 1 });
                    },
                };
                jQuery.ajax(r);
            });
        },
        updateURL: function (e) {
            let t = pdappAdminUrl + "admin.php?page=product-designer-app-" + e;
            window.history.pushState(null, "", t);
        },
        changeThumb: function (e, t, n, a) {
            if ("image/png" != e.type && "image/jpg" != e.type && "image/jpeg" != e.type)
                return (
                    (pdappVueApp.toast = { show: !0, type: "error", text: t.msg.file_type_error + " PNG, JPG, JPEG" }),
                    !1
                );
            let r = document.createElement("canvas"),
                o = r.getContext("2d"),
                i = new Image();
            (i.onload = function () {
                (r.width = void 0 === a ? 500 : a), (r.height = r.width);
                let e = r.width,
                    t = r.width,
                    d = 0,
                    s = 0;
                i.width > i.height
                    ? ((t = (r.width * i.height) / i.width), (d = Math.round((r.width - t) / 2)))
                    : ((e = (r.width * i.width) / i.height), (s = Math.round((r.width - e) / 2))),
                    o.drawImage(i, s, d, e, t);
                let p = r.toDataURL();
                setTimeout(() => {
                    "function" == typeof n && n(p);
                });
            }),
                (i.src = URL.createObjectURL(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)));
        },
        getTransform: function (e) {
            let t = e.css("transform");
            return (void 0 !== t && "none" != t) || (t = e.attr("transform")), void 0 !== t && "none" != t ? t : "";
        },
        toPx: function (e = 0, t = "in", n = 300) {
            let a = 0;
            return (
                (a = "in" == t ? parseFloat(e) * n : "cm" == t ? (parseFloat(e) / 2.54) * n : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
        toCm: function (e = 0, t = "in", n = 300) {
            let a = 0;
            return (
                (a = "in" == t ? 2.54 * parseFloat(e) : "px" == t ? (parseFloat(e) / n) * 2.54 : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
        toIn: function (e = 0, t = "cm", n = 300) {
            let a = 0;
            return (
                (a = "cm" == t ? parseFloat(e) / 2.54 : "px" == t ? parseFloat(e) / n : parseFloat(e)),
                parseFloat(a.toFixed(3))
            );
        },
    },
    design: {
        renderPNGs: async function (e, t, n, a) {
            let r = this;
            if ((void 0 === a && (a = 0), void 0 === e[a])) return !1;
            if ("" != e[a].png) "function" == typeof n && n(a, e[a].svg, e[a].png), a++, r.renderPNGs(e, t, n, a);
            else {
                if ("none" == e[a].svg)
                    return "function" == typeof n && n(a, "none", ""), a++, r.renderPNGs(e, t, n, a), !1;
                if (-1 === e[a].svg.indexOf(";base64,")) {
                    let o =
                        pdappAdminUrl + "admin-ajax.php?action=pdapp-file-download&admin=1&pdapp-nonce=" + pdappNonce;
                    try {
                        let i = await pdappAPI.tool.postData(o, { file: e[a].svg });
                        if (t.length && -1 !== i.data.indexOf("data:image/svg+xml;base64,")) {
                            let o = i.data.split(";base64,"),
                                d = pdappAPI.tool.decodeBase64(o[1]),
                                s = [],
                                p = new DOMParser().parseFromString(d, "image/svg+xml");
                            if (
                                (jQuery(p.documentElement).find("span.pdapp-team-value").remove(),
                                jQuery(p.documentElement)
                                    .find("g.pdapp-target")
                                    .each(function () {
                                        jQuery(this).hasClass("pdapp-team") && s.push(jQuery(this)[0].outerHTML),
                                            jQuery(this).hasClass("pdapp-team") && jQuery(this).remove();
                                    }),
                                (e[a].svg =
                                    "data:image/svg+xml;base64," +
                                    pdappAPI.tool.encodeBase64(jQuery(p.documentElement)[0].outerHTML)),
                                0 == jQuery(p.documentElement).find("g").length)
                            ) {
                                for (let e = 0; e < s.length; e++) jQuery(p.documentElement).append(s[e]);
                                return (
                                    jQuery(p.documentElement).attr(
                                        "height",
                                        jQuery(p.documentElement).attr("data-height")
                                    ),
                                    jQuery(p.documentElement).attr(
                                        "width",
                                        jQuery(p.documentElement).attr("data-width")
                                    ),
                                    "function" == typeof n && n(a, "none", "", jQuery(p.documentElement)[0].outerHTML),
                                    a++,
                                    r.renderPNGs(e, t, n, a),
                                    !1
                                );
                            }
                            jQuery(p.documentElement).find("g").remove();
                            for (let e = 0; e < s.length; e++) jQuery(p.documentElement).append(s[e]);
                            jQuery(p.documentElement).attr("height", jQuery(p.documentElement).attr("data-height")),
                                jQuery(p.documentElement).attr("width", jQuery(p.documentElement).attr("data-width")),
                                "function" == typeof n && n(a, e[a].svg, "", jQuery(p.documentElement)[0].outerHTML);
                        } else e[a].svg = i.data;
                    } catch (o) {
                        return "function" == typeof n && n(a, "", ""), a++, r.renderPNGs(e, t, n, a), !1;
                    }
                }
                canvas = document.createElement("canvas");
                let o = canvas.getContext("2d"),
                    i = new Image();
                (i.onload = function () {
                    (canvas.width = i.width), (canvas.height = i.height), o.drawImage(i, 0, 0);
                    let d = canvas.toDataURL("image/png");
                    "function" == typeof n && n(a, e[a].svg, d),
                        setTimeout(() => {
                            a++, r.renderPNGs(e, t, n, a);
                        }, 200);
                }),
                    (i.src = e[a].svg);
            }
        },
        renderTeams: function (e, t, n) {
            this.renderTeam(e, t, 0, n);
        },
        renderTeam: function (e, t, n, a) {
            let r = this;
            if (void 0 === t[n]) return !1;
            let o = new DOMParser().parseFromString(e, "image/svg+xml"),
                i = jQuery(o.documentElement),
                d = 1,
                s = i.attr("viewBox");
            if (void 0 !== s) {
                let e = parseFloat(s.split(" ")[2]),
                    t = parseFloat(s.split(" ")[3]),
                    n = i.width(),
                    a = i.height();
                (parseInt(n / d) == parseInt(e) && parseInt(a / d) == parseInt(t)) ||
                    (d = e / t > n / a ? n / e : a / t);
            }
            if (void 0 !== t[n].name) {
                let e = i.find("g.pdapp-team-name");
                if (e.length) {
                    let a = [],
                        r = e[0].getBoundingClientRect();
                    if (0 != r.width) {
                        let t = pdappAPI.tool.getTransform(e);
                        "" != t && (a = t.replace("matrix(", "").replace(")", "").replaceAll(" ", "").split(","));
                    }
                    e.find("text").text(t[n].name),
                        setTimeout(() => {
                            if (6 == a.length) {
                                let t = e[0].getBoundingClientRect();
                                if (0 != t.width) {
                                    let n = ((t.width - r.width) / 2 / d).toFixed(3);
                                    (n = parseFloat(n)),
                                        (a[4] = parseFloat(a[4]) - n),
                                        e.attr("transform", "matrix(" + a.join(", ") + ")");
                                }
                            }
                        }, 10);
                }
            }
            if (void 0 !== t[n].number) {
                let e = i.find("g.pdapp-team-number");
                e.length &&
                    e.each(function () {
                        let e = jQuery(this),
                            a = [],
                            r = e[0].getBoundingClientRect();
                        if (0 != r.width) {
                            let t = pdappAPI.tool.getTransform(e);
                            "" != t && (a = t.replace("matrix(", "").replace(")", "").replaceAll(" ", "").split(","));
                        }
                        e.find("text").text(t[n].number),
                            setTimeout(() => {
                                if (6 == a.length) {
                                    let t = e[0].getBoundingClientRect();
                                    if (0 != t.width) {
                                        let n = ((t.width - r.width) / 2 / d).toFixed(3);
                                        (n = parseFloat(n)),
                                            (a[4] = parseFloat(a[4]) - n),
                                            e.attr("transform", "matrix(" + a.join(", ") + ")");
                                    }
                                }
                            }, 10);
                    });
            }
            setTimeout(() => {
                let o = i[0].outerHTML;
                o = "data:image/svg+xml;base64," + pdappAPI.tool.encodeBase64(o);
                let d = { svg: o, png: "" };
                canvas = document.createElement("canvas");
                let s = canvas.getContext("2d"),
                    p = new Image();
                (p.onload = function () {
                    (canvas.width = p.width),
                        (canvas.height = p.height),
                        s.drawImage(p, 0, 0),
                        (d.png = canvas.toDataURL("image/png")),
                        "function" == typeof a && a(n, d),
                        setTimeout(() => {
                            r.renderTeam(e, t, n + 1, a);
                        }, 200);
                }),
                    (p.src = o);
            }, 50);
        },
        downloadFile: async function (e, t, n, a, r) {
            let o = document.createElement("a");
            (o.download = t + "." + n), (o.href = e), o.click(), o.remove(), "function" == typeof r && r();
        },
    },
};