import { __awaiter } from "tslib";
import dot from "dot-object";
export const generateFieldName = (field, prefix = "") => field.type === "object"
    ? Object.entries(field.properties).flatMap(([fieldName, field]) => generateFieldName(field, prefix ? `${prefix}.${fieldName}` : fieldName))
    : field.type === "list"
        ? generateFieldName(field.items, `${prefix}.0`)
        : [[prefix, field.type]];
export const sourceFetcher = (currentSource, source) => __awaiter(void 0, void 0, void 0, function* () {
    const valid = currentSource.params
        .filter((field) => field.required)
        .every((field) => { var _a; return ((_a = source === null || source === void 0 ? void 0 : source.params) === null || _a === void 0 ? void 0 : _a[field.slug]) !== ""; });
    if (!valid)
        return Promise.reject("some required parameter is empty");
    const params = ["GET", "OPTION"].includes(currentSource.method) && source.type === "list"
        ? `?${Object.entries((source === null || source === void 0 ? void 0 : source.params) || {})
            .map((keyValue) => keyValue.join("="))
            .join("&")}`
        : "";
    const body = ["POST", "PUT", "PATCH", "DELETE"].includes(currentSource.method)
        ? Object.keys((source === null || source === void 0 ? void 0 : source.params) || {}).reduce((formData, key) => {
            formData.append(key, source === null || source === void 0 ? void 0 : source.params[key]);
            return formData;
        }, new FormData())
        : undefined;
    const url = new URL(currentSource.host);
    return fetch([
        "/api/cors/" + url.host + url.pathname + url.search,
        currentSource.path,
        params,
    ].join(""), {
        headers: currentSource.headers,
        method: currentSource.method,
        body,
    })
        .then((resp) => resp.json())
        .then((resp) => currentSource.selector ? dot.pick(currentSource.selector, resp) : resp);
});
export const openAPISourceFetcher = (source, customSource) => {
    const fields = customSource.columns.type === "object"
        ? generateFieldName(customSource.columns).map(([field, type]) => ({
            id: field,
            display: field,
            type,
        }))
        : [];
    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- for type inference
    const fetch = (params) => {
        return sourceFetcher(customSource, source);
    };
    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- for type inference
    const fetchList = (params) => __awaiter(void 0, void 0, void 0, function* () {
        return sourceFetcher(customSource, source).then((result) => result || []);
    });
    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- for type inference
    const store = (params) => {
        return sourceFetcher(customSource, source);
    };
    // eslint-disable-next-line @typescript-eslint/no-unused-vars -- for type inference
    const notImplemeted = (params) => __awaiter(void 0, void 0, void 0, function* () { return Promise.reject(Error("not implemented")); });
    return {
        type: "openapi",
        params: customSource.params,
        inputParams: customSource.params,
        fields,
        fetch,
        fetchList,
        store,
        del: notImplemeted,
    };
};
//# sourceMappingURL=openAPISourceHooks.js.map