/**
* PDA v1.1.2
* (c) 2024 Product Designer App
* @license GPLv3
* Artwork
* Free version
* Unminify code for Dev
**/
var pdappAPI = {
    folder: {
        getFolderBreads: function (e, t, r, o) {
            t.title = this.getTitle(t.title);
            let n = [];
            this.getFolderBread(e, t, r, function (e) {
                n.push(e), 0 == e.id && (n.reverse(), "function" == typeof o && o(n));
            });
        },
        getFolderBread: function (e, t, r, o) {
            if (0 != r) {
                let n = 0;
                for (let t in e)
                    if (t != r)
                        for (let a = 0; a < e[t].length; a++)
                            e[t][a].id == r &&
                                ((n = e[t][a].pid),
                                "function" == typeof o &&
                                    o({ id: e[t][a].id, pid: e[t][a].pid, title: this.getTitle(e[t][a].title) }));
                0 == n
                    ? "function" == typeof o && o(t)
                    : this.getFolderBread(e, t, n, function (e) {
                          "function" == typeof o && o(e);
                      });
            } else "function" == typeof o && o(t);
        },
        getTitle: function (e) {
            let t = "Untitled";
            if (void 0 !== e[pdappLangDefault] && "" != e[pdappLangDefault]) t = e[pdappLangDefault];
            else for (let r in e) "" != e[r] && (t = e[r]);
            return t;
        },
    },
    tool: {
        uID: function () {
            return Math.floor(1e3 * Math.random()) + "-" + Math.floor(1e5 * Math.random());
        },
        toDate: function (e) {
            let t = new Date(e.replaceAll("-", "/")).toLocaleTimeString(),
                r = e.split(" "),
                o = t.split(" "),
                n = o[0].split(":");
            return void 0 !== o[1] ? r[0] + " " + n[0] + ":" + n[1] + o[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 o = {
                    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, o) {
                        r({ msg: o, error: 1 });
                    },
                };
                jQuery.ajax(o);
            });
        },
        postData: function (e, t) {
            return new Promise(function (r, o) {
                let n = {
                    method: "POST",
                    url: e,
                    dataType: "json",
                    data: t,
                    success: function (e) {
                        0 == e
                            ? o({ msg: "403 Forbidden", error: 1 })
                            : (void 0 === e.error && (e.error = 1),
                              0 === e.error ? r({ msg: e.msg, data: e.data }) : o({ msg: e.msg, error: e.error }));
                    },
                    error: function (e, t, r) {
                        o({ msg: r, error: 1 });
                    },
                };
                jQuery.ajax(n);
            });
        },
        uploadData: function (e, t, r) {
            return new Promise(function (o, n) {
                let a = {
                    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 ? o({ 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(a);
            });
        },
        updateURL: function (e) {
            let t = pdappAdminUrl + "admin.php?page=product-designer-app-" + e;
            window.history.pushState(null, "", t);
        },
        changeThumb(e, t, r) {
            if ("image/png" != e.type && "image/jpg" != e.type && "image/jpeg" != e.type && "image/svg+xml" != e.type)
                return !1;
            let o = document.createElement("canvas"),
                n = o.getContext("2d"),
                a = new Image();
            return (
                (a.onload = function () {
                    (r = void 0 === r ? 500 : r),
                        (o.width = r),
                        (o.height = (r * a.height) / a.width),
                        n.drawImage(a, 0, 0, o.width, o.height);
                    let e = o.toDataURL();
                    setTimeout(() => {
                        "function" == typeof t && t(e);
                    });
                }),
                (a.src = URL.createObjectURL(e)),
                !0
            );
        },
    },
};