/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Settings
* Free version
* Unminify code for Dev
**/
var pdappAPI = {
    tool: {
        toDate: function (e) {
            let t = new Date(e.replaceAll("-", "/")).toLocaleTimeString(),
                r = e.split(" "),
                a = t.split(" "),
                n = a[0].split(":");
            return void 0 !== a[1] ? r[0] + " " + n[0] + ":" + n[1] + a[1] : e;
        },
        setLocalVar: function (e, t) {
            localStorage.setItem(e, t);
        },
        getLocalVar: function (e, t = "") {
            var r = localStorage.getItem(e);
            return null != r ? r : t;
        },
        getData: function (e) {
            return new Promise(function (t, r) {
                let a = {
                    method: "GET",
                    url: e,
                    dataType: "json",
                    success: function (e) {
                        0 == e
                            ? r({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? t({ msg: e.msg, data: e.data }) : r({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, a) {
                        r({ msg: a, error: 1 });
                    },
                };
                jQuery.ajax(a);
            });
        },
        postData: function (e, t) {
            return new Promise(function (r, a) {
                let n = {
                    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 ? r({ msg: e.msg, data: e.data }) : a({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, r) {
                        a({ msg: r, error: 1 });
                    },
                };
                jQuery.ajax(n);
            });
        },
        uploadData: function (e, t, r) {
            return new Promise(function (a, n) {
                let o = {
                    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 r && r(t);
                                    }
                                },
                                !1
                            ),
                            e
                        );
                    },
                    success: function (e) {
                        0 == e
                            ? n({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? a({ msg: e.msg, data: e.data }) : n({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, r) {
                        n({ msg: r, error: 1 });
                    },
                };
                jQuery.ajax(o);
            });
        },
        updateURL: function (e) {
            let t = pdappAdminUrl + "admin.php?page=product-designer-app-" + e;
            window.history.pushState(null, "", t);
        },
        loadFontFace: function (e, t) {
            let r = document.createElement("style");
            r.appendChild(document.createTextNode(e)),
                (r.onload = function () {
                    setTimeout(() => {
                        "function" == typeof t && t();
                    }, 500);
                }),
                document.head.appendChild(r);
        },
        genFontThumb: function (e, t) {
            let r = document.createElement("canvas");
            (r.width = 50 * e.title.length + 40), (r.height = 80);
            let a = r.getContext("2d");
            (a.font = "50px " + e.title),
                (a.fillStyle = "black"),
                (a.textAlign = "center"),
                a.fillText(e.title, r.width / 2, r.height / 2);
            let n = a.getImageData(0, 0, r.width, r.height).data,
                o = r.width,
                i = r.height,
                p = 0,
                l = 0;
            for (let e = 0; e < r.height; e++)
                for (var s = 0; s < r.width; s++) {
                    n[4 * (e * r.width + s) + 3] > 0 &&
                        ((o = Math.min(o, s)), (i = Math.min(i, e)), (p = Math.max(p, s)), (l = Math.max(l, e)));
                }
            let c = p - o + 1,
                d = l - i + 1,
                u = document.createElement("canvas"),
                h = u.getContext("2d");
            (u.width = c),
                (u.height = d),
                h.drawImage(r, o, i, c, d, 0, 0, c, d),
                "function" == typeof t &&
                    setTimeout(() => {
                        t(e, u.toDataURL());
                    }, 100);
        },
        textThumb: function (e, t, r) {
            let a = e[0].outerHTML,
                n = document.createElement("canvas"),
                o = n.getContext("2d"),
                i = new Image();
            (i.onload = function () {
                let e = i.width,
                    a = i.height;
                a > t && ((e = t * (e / a)), (a = t)),
                    (n.width = e),
                    (n.height = a),
                    o.drawImage(i, 0, 0, i.width, i.height, 0, 0, e, a),
                    (n.width = i.width),
                    (n.height = i.height),
                    o.drawImage(i, 0, 0, n.width, n.height);
                let p = o.getImageData(0, 0, n.width, n.height).data,
                    l = n.width,
                    s = n.height,
                    c = 0,
                    d = 0;
                for (let e = 0; e < n.height; e++)
                    for (var u = 0; u < n.width; u++) {
                        p[4 * (e * n.width + u) + 3] > 0 &&
                            ((l = Math.min(l, u)), (s = Math.min(s, e)), (c = Math.max(c, u)), (d = Math.max(d, e)));
                    }
                let h = c - l + 1,
                    m = d - s + 1,
                    g = document.createElement("canvas"),
                    f = g.getContext("2d");
                (g.width = h),
                    (g.height = m),
                    f.drawImage(n, l, s, h, m, 0, 0, h, m),
                    setTimeout(() => {
                        "function" == typeof r && r(g.toDataURL());
                    }, 1);
            }),
                (i.src = "data:image/svg+xml;base64," + pdappAPI.tool.encodeBase64(a));
        },
        responsiveSvgText: function (e) {
            let t = e.find("g.pdapp-target"),
                r = e[0].getBoundingClientRect(),
                a = t[0].getBoundingClientRect();
            e.attr("width", a.width.toFixed(3) + "px"), e.attr("height", a.height.toFixed(3) + "px");
            let n = r.left - a.left,
                o = r.top - a.top,
                i = t.css("transform");
            if (void 0 !== i && "none" != i) {
                let e = i.replace("matrix(", "").replace(")", "").replaceAll(" ", "").split(",");
                6 == e.length && ((n += parseFloat(e[4])), (o += parseFloat(e[5])));
            }
            t.attr("style", "transform: matrix(1, 0, 0, 1, " + n.toFixed(3) + ", " + o.toFixed(3) + ")");
        },
        encodeBase64: function (e) {
            let t = unescape(encodeURIComponent(e));
            return btoa(t);
        },
        decodeBase64: function (e) {
            return (e = e.replace(/\s/g, "")), decodeURIComponent(escape(atob(e)));
        },
        colorPicker: function (e, t, r) {
            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)",
                ],
            }),
                jQuery(e).on("move.spectrum", function (e, t) {
                    let a = t.toHexString();
                    "function" == typeof r && r(e.target, a);
                }),
                setTimeout(() => {
                    jQuery(".pdapp-text-groups .pdapp-list-document").text(t.document_color),
                        jQuery(".pdapp-text-groups .pdapp-list-selection").text(t.selection_color);
                }, 1);
        },
    },
    arc: {
        pdappCircleType: null,
        create: function (e, t, r) {
            let a = jQuery("#" + e + " .pdapp-arc"),
                n = t.text.replaceAll(" ", "&nbsp;");
            a.html(n);
            let o = new CircleType(a[0]);
            0 != t.arc.radius && o.radius(t.arc.radius), -1 == t.arc.dir && o.dir(parseInt(t.arc.dir));
            let i = this;
            setTimeout(() => {
                (i.pdappCircleType = o),
                    setTimeout(() => {
                        let e = [];
                        a.find("span").each(function () {
                            let t = jQuery(this).html();
                            t = t.replace("&nbsp;", "").replace("&amp;", "");
                            let r = jQuery(this).css("transform");
                            if ("none" != r) {
                                let a = jQuery(this).css("transform-origin");
                                e.push({ text: t, transform: r, transformO: a });
                            }
                        }),
                            e.length && "function" == typeof r && r(e),
                            0 == e.length &&
                                setTimeout(() => {
                                    a.find("span").each(function () {
                                        let t = jQuery(this).html();
                                        t = t.replace("&nbsp;", "").replace("&amp;", "");
                                        let r = jQuery(this).css("transform"),
                                            a = jQuery(this).css("transform-origin");
                                        e.push({ text: t, transform: r, transformO: a });
                                    }),
                                        "function" == typeof r && r(e);
                                }, 50);
                    }, 1);
            }, 5);
        },
        update: function (e, t, r) {
            let a = jQuery("#" + e + " .pdapp-arc"),
                n = t.arc.radius;
            if (null != pdappAPI.arc.pdappCircleType)
                pdappAPI.arc.pdappCircleType.radius(n),
                    -1 == t.arc.dir ? pdappAPI.arc.pdappCircleType.dir(-1) : pdappAPI.arc.pdappCircleType.dir(1);
            else {
                let e = t.text.replaceAll(" ", "&nbsp;");
                a.html(e),
                    (pdappAPI.arc.pdappCircleType = new CircleType(a[0]).radius(n)),
                    -1 == t.arc.dir ? pdappAPI.arc.pdappCircleType.dir(-1) : pdappAPI.arc.pdappCircleType.dir(1);
            }
            setTimeout(() => {
                let e = [];
                a.find("span").each(function () {
                    let t = jQuery(this).html();
                    t = t.replace("&nbsp;", "").replace("&amp;", "");
                    let r = jQuery(this).css("transform");
                    if ("none" != r) {
                        let a = jQuery(this).css("transform-origin");
                        e.push({ text: t, transform: r, transformO: a });
                    }
                }),
                    e.length && "function" == typeof r && r(e),
                    0 == e.length &&
                        setTimeout(() => {
                            a.find("span").each(function () {
                                let t = jQuery(this).html();
                                t = t.replace("&nbsp;", "").replace("&amp;", "");
                                let r = jQuery(this).css("transform"),
                                    a = jQuery(this).css("transform-origin");
                                e.push({ text: t, transform: r, transformO: a });
                            }),
                                "function" == typeof r && r(e);
                        }, 50);
            }, 5);
        },
    },
};