"use strict";
exports.id = 284;
exports.ids = [284];
exports.modules = {

/***/ 284:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "aC": () => (/* binding */ useAuth),
/* harmony export */   "zP": () => (/* binding */ AuthDataProvider)
/* harmony export */ });
/* unused harmony export AuthDataContext */
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(997);
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6689);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var react_toastify__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(3590);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4957);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(cookie_cutter__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1853);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(next_router__WEBPACK_IMPORTED_MODULE_4__);
/* harmony import */ var _UserDataContext__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(3401);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([react_toastify__WEBPACK_IMPORTED_MODULE_2__]);
react_toastify__WEBPACK_IMPORTED_MODULE_2__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];






const AuthDataContext = /*#__PURE__*/ (0,react__WEBPACK_IMPORTED_MODULE_1__.createContext)(undefined);
const AuthDataProvider = ({ children  })=>{
    const router = (0,next_router__WEBPACK_IMPORTED_MODULE_4__.useRouter)();
    const { exit  } = (0,_UserDataContext__WEBPACK_IMPORTED_MODULE_5__/* .useUser */ .aF)();
    const login = async (user)=>{
        cookie_cutter__WEBPACK_IMPORTED_MODULE_3___default().set("token", user.token, {
            expires: "86400",
            path: "/"
        });
    };
    const logout = async ()=>{
        react_toastify__WEBPACK_IMPORTED_MODULE_2__.toast.success("خروج با موفقیت انجام شد!");
        await cookie_cutter__WEBPACK_IMPORTED_MODULE_3___default().set("token", "", {
            expires: "Thu, 01 Jan 1970 00:00:01 GMT"
        });
        await exit();
        await router.push("/Login");
    };
    const value = (0,react__WEBPACK_IMPORTED_MODULE_1__.useMemo)(()=>({
            // cookies,
            login,
            logout
        }), []);
    return /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(AuthDataContext.Provider, {
        value: value,
        children: children
    });
};
const useAuth = ()=>{
    return (0,react__WEBPACK_IMPORTED_MODULE_1__.useContext)(AuthDataContext);
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 3401:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "aF": () => (/* binding */ useUser),
/* harmony export */   "i1": () => (/* binding */ UserDataProvider)
/* harmony export */ });
/* unused harmony export UserDataContext */
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(997);
/* harmony import */ var react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6689);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4957);
/* harmony import */ var cookie_cutter__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(cookie_cutter__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(1853);
/* harmony import */ var next_router__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(next_router__WEBPACK_IMPORTED_MODULE_3__);




const UserDataContext = /*#__PURE__*/ (0,react__WEBPACK_IMPORTED_MODULE_1__.createContext)(undefined);
const UserDataProvider = ({ children  })=>{
    const [userData, setUserData] = (0,react__WEBPACK_IMPORTED_MODULE_1__.useState)({
        data: {
            id: "",
            name: "",
            email: "",
            created_at: "",
            updated_at: ""
        },
        status: false
    });
    const refetch = async ()=>{
        const token = cookie_cutter__WEBPACK_IMPORTED_MODULE_2___default().get("token");
        if (token && token.length > 0) {
            return await fetch("/api/user/me", {
                headers: {
                    Authorization: "Bearer " + token
                }
            }).then(async (r)=>{
                if (r.status === 200) {
                    const item = await r.json();
                    await setUserData({
                        data: {
                            ...item
                        },
                        status: true
                    });
                } else {
                    setUserData({
                        ...userData,
                        status: false
                    });
                    cookie_cutter__WEBPACK_IMPORTED_MODULE_2___default().set("token", "", {
                        expires: "Thu, 01 Jan 1970 00:00:01 GMT",
                        path: "/"
                    });
                }
            });
        }
        return false;
    };
    const permissions = ()=>{
        let perms = [];
        userData.data.roles.map((role)=>{
            role.permissions.map((item)=>{
                perms.push(item);
            });
        });
        return perms;
    };
    const exit = ()=>{
        setUserData({
            data: {
                id: "",
                name: "",
                email: "",
                created_at: "",
                updated_at: ""
            },
            status: false
        });
    };
    const value = (0,react__WEBPACK_IMPORTED_MODULE_1__.useMemo)(()=>({
            userData,
            refetch,
            exit,
            permissions
        }), [
        userData
    ]);
    return /*#__PURE__*/ react_jsx_runtime__WEBPACK_IMPORTED_MODULE_0__.jsx(UserDataContext.Provider, {
        value: value,
        children: children
    });
};
const useUser = ()=>{
    return (0,react__WEBPACK_IMPORTED_MODULE_1__.useContext)(UserDataContext);
};


/***/ })

};
;