"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertBlobToString = void 0;
const tslib_1 = require("tslib");
const react_1 = tslib_1.__importDefault(require("react"));
const reducers_1 = require("../../common/reducers");
const apis_1 = require("../services/apis");
const api_1 = require("../../common/api");
const utils_1 = require("../../common/utils");
const { addAlert } = reducers_1.alertsSlice.actions;
const convertBlobToString = (blob) => {
    return new Promise((resolve) => {
        const reader = new FileReader();
        reader.readAsText(blob, 'UTF-8');
        reader.onload = (readerEvent) => {
            var _a;
            const content = (_a = readerEvent.target) === null || _a === void 0 ? void 0 : _a.result;
            resolve(content);
        };
    });
};
exports.convertBlobToString = convertBlobToString;
const getManifestsInfo = (customManifests, clusterId) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    const manifestsExtended = [];
    for (const manifest of customManifests) {
        try {
            const { data: yamlContent } = yield apis_1.ClustersAPI.getManifestContent(clusterId, manifest.folder || 'manifests', manifest.fileName || '');
            const yamlString = yield (0, exports.convertBlobToString)(yamlContent);
            manifestsExtended.push(Object.assign(Object.assign({}, manifest), { folder: manifest.folder || 'manifests', fileName: manifest.fileName || '', yamlContent: yamlString }));
        }
        catch (e) {
            manifestsExtended.push(Object.assign(Object.assign({}, manifest), { folder: manifest.folder || 'manifests', fileName: manifest.fileName || '', yamlContent: '' }));
        }
    }
    return manifestsExtended;
});
const useClusterCustomManifests = (clusterId, extendedVersion) => {
    const [customManifests, setCustomManifests] = react_1.default.useState();
    const [error, setError] = react_1.default.useState('');
    const [extendedManifestsLoaded, setExtendedManifestsLoaded] = react_1.default.useState(false);
    const fetchCustomManifests = react_1.default.useCallback(() => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        try {
            if (clusterId !== '') {
                const { data } = yield apis_1.ClustersAPI.getManifests(clusterId);
                if (extendedVersion) {
                    yield getManifestsInfo(data, clusterId).then((manifests) => {
                        setCustomManifests(manifests);
                        setExtendedManifestsLoaded(true);
                    });
                }
                else {
                    const manifestsExtended = data.map((manifest) => (Object.assign(Object.assign({}, manifest), { folder: manifest.folder || 'manifests', fileName: manifest.fileName || '', yamlContent: '' })));
                    setCustomManifests(manifestsExtended);
                }
            }
        }
        catch (e) {
            setError((0, utils_1.getErrorMessage)(e));
            // report error systematically at one place show defaults instead
            (0, api_1.handleApiError)(e, () => addAlert({
                title: 'Failed to retrieve cluster custom manifests',
                message: (0, api_1.getApiErrorMessage)(e),
            }));
        }
    }), [clusterId, extendedVersion]);
    react_1.default.useEffect(() => {
        void fetchCustomManifests();
    }, [fetchCustomManifests]);
    return {
        customManifests,
        error,
        isLoading: !error && !customManifests && (extendedVersion ? !extendedManifestsLoaded : true),
    };
};
exports.default = useClusterCustomManifests;
//# sourceMappingURL=useClusterCustomManifests.js.map