"use strict";
(() => {
var exports = {};
exports.id = 8937;
exports.ids = [8937];
exports.modules = {

/***/ 2261:
/***/ ((module) => {

module.exports = require("serverless-mysql");

/***/ }),

/***/ 4300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 6113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 7147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 3685:
/***/ ((module) => {

module.exports = require("http");

/***/ }),

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

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2224);
/* harmony import */ var _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2163);
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6825);
/* harmony import */ var _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(9132);
/* harmony import */ var _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3__);
// @ts-check
/**
 * ==============================================================================
 * Imports
 * ==============================================================================
 */ const fs = __webpack_require__(7147);




/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
 * API handler
 * ==============================================================================
 * @type {import("next").NextApiHandler}
 */ async function handler(req, res) {
    /**
     * Check method
     *
     * @description Check request method and return if invalid
     */ if (req.method !== "POST") return res.json({
        msg: "Failed!"
    });
    /**
     * User auth
     *
     * @description Authenticate user
     */ const user = await _functions_backend_userAuth__WEBPACK_IMPORTED_MODULE_2___default()(req, res, true);
    if (!user) {
        return res.json({
            success: false,
            msg: "Unauthorized"
        });
    }
    /**
     * User auth
     *
     * @description Authenticate user
     */ const sanitizedReqBody = req.body;
    const { id , db_slug , db_full_name  } = sanitizedReqBody;
    if (!db_full_name.match(`user_${user.id}_`)) {
        throw new Error("Database doesn't belong to user");
    }
    await _package_shared_functions_backend_noDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_3___default()(`DROP DATABASE \`${db_full_name}\``);
    await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default()(`DELETE FROM user_databases WHERE id = ?`, [
        id
    ]);
    await _package_shared_utils_backend_global_db_DB_HANDLER__WEBPACK_IMPORTED_MODULE_0___default()(`DELETE FROM user_users WHERE user_id = ? AND \`database\` = ?`, [
        user.id,
        db_slug
    ]);
    try {
        /**
         * Send Response
         *
         * @description Send a boolean response
         */ const userSchemaMainFilePath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${user.id}/main.json`;
        /** @type {import("@/package-shared/types").DSQL_DatabaseSchemaType[]} */ let userSchemaData = JSON.parse(fs.readFileSync(userSchemaMainFilePath, "utf8"));
        /** @type {any} */ let targetDatabaseIndex;
        let targetDatabase = userSchemaData.filter((databaseObject, index)=>{
            if (databaseObject.dbFullName === db_full_name) {
                targetDatabaseIndex = index;
                return true;
            }
        });
        if (targetDatabase[0].childDatabase) {
            const parentDb = userSchemaData.filter((db)=>db.dbFullName === targetDatabase[0].childDatabaseDbFullName)[0];
            const targetChildIndex = parentDb.childrenDatabases?.findIndex((val)=>val.dbFullName === targetDatabase[0].childDatabaseDbFullName);
            if (parentDb.childrenDatabases && typeof targetChildIndex == "number") {
                parentDb.childrenDatabases.splice(targetChildIndex, 1);
            }
        }
        userSchemaData.splice(targetDatabaseIndex, 1);
        fs.writeFileSync(`${userSchemaMainFilePath}`, JSON.stringify(userSchemaData), "utf8");
        ////////////////////////////////////////
        res.json({
            success: true
        });
    ////////////////////////////////////////
    } catch (/** @type {any} */ error) {
        ////////////////////////////////////////
        _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_1___default()({
            component: "/api/deleteUserDatabase/main-catch-error",
            message: error.message,
            user: user
        });
        res.json({
            success: false,
            msg: "File Updates Failed"
        });
    }
}


/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [2224,2163,6825,3017,7487,9132], () => (__webpack_exec__(2005)));
module.exports = __webpack_exports__;

})();