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

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

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "o": () => (/* binding */ HttpError),
/* harmony export */   "u": () => (/* binding */ errorCode)
/* harmony export */ });
class HttpError extends Error {
    constructor(code, status){
        super(code);
        this.code = code;
        this.status = status;
    }
}
const errorCode = {
    LOGIN_FAILED: new HttpError("LOGIN_FAILED", 401),
    UNAUTHORIZED_REQUEST: new HttpError("UNAUTHORIZED_REQUEST", 401),
    RESPONSE_FORMAT_ERROR: new HttpError("RESPONSE_FORMAT_ERROR", 400)
};


/***/ }),

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

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "Z": () => (__WEBPACK_DEFAULT_EXPORT__),
/* harmony export */   "b": () => (/* binding */ axiosInstance)
/* harmony export */ });
/* harmony import */ var _errorCode__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5373);
/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2167);
/* harmony import */ var axios__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(axios__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var next_config__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4558);
/* harmony import */ var next_config__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(next_config__WEBPACK_IMPORTED_MODULE_2__);



const { serverRuntimeConfig  } = next_config__WEBPACK_IMPORTED_MODULE_2___default()();
const VULCAN_SQL_HOST = serverRuntimeConfig.vulcanSQLHost;
const axiosInstance = axios__WEBPACK_IMPORTED_MODULE_1___default().create({
    baseURL: VULCAN_SQL_HOST,
    responseType: "json",
    headers: {
        "Content-Type": "application/json"
    }
});
const getAuthorization = (ctx)=>{
    return {
        headers: {
            Authorization: ctx.apiToken
        }
    };
};
axiosInstance.interceptors.response.use((response)=>response
, (error)=>{
    const errorInformation = {
        code: error.code,
        requestUrl: `${error.config.baseURL}${error.config.url}`,
        method: error.config.method,
        status: error.response.status,
        data: error.response.data || ""
    };
    console.error("\x1b[31m%s\x1b[0m", `ERROR: ${JSON.stringify(errorInformation)}`);
    return Promise.reject(error);
});
class VulcanSQLAdapter {
    async getAuthType() {
        try {
            const { data  } = await axiosInstance.get(`/auth/available-types`);
            const authType = data.find((type)=>[
                    "basic",
                    "password-file"
                ].includes(type)
            ) || "";
            return authType;
        } catch (err) {
            return null;
        }
    }
    async getInitToken(params) {
        const { data  } = await axiosInstance.post(`/auth/token`, params);
        return data.token;
    }
    async getUserProfile(ctx) {
        try {
            const { data  } = await axiosInstance.get(`/auth/user-profile`, getAuthorization(ctx));
            return data;
        } catch (err) {
            throw _errorCode__WEBPACK_IMPORTED_MODULE_0__/* .errorCode.LOGIN_FAILED */ .u.LOGIN_FAILED;
        }
    }
    async getSchemas(ctx) {
        const { data  } = await axiosInstance.get(`/catalog/schemas`, getAuthorization(ctx));
        return data;
    }
    getSchema = async (ctx, slug)=>{
        const { data  } = await axiosInstance.get(`/catalog/schemas/${slug}`, getAuthorization(ctx));
        return data;
    };
    async getPreviewData(ctx, args) {
        const { slug , filter  } = args;
        const schema = await this.getSchema(ctx, slug);
        const actualPath = Object.keys(filter).reduce((result, key)=>{
            if (!filter[key]) return result;
            const param = `:${key}`;
            const isParam = result.includes(param);
            const querySymbol = result.includes("?") ? "&" : "?";
            return isParam ? result.replace(param, filter[key]) : `${result}${querySymbol}${key}=${filter[key]}`;
        }, `/api${schema.urlPath}`);
        const actualUrl = `${VULCAN_SQL_HOST}${actualPath}`;
        console.log("actualUrl: ", actualUrl);
        const { data  } = await axiosInstance.get(actualPath, getAuthorization(ctx));
        return {
            schema: {
                ...schema,
                actualPath,
                actualUrl
            },
            data
        };
    }
}
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = (new VulcanSQLAdapter());


/***/ })

};
;