"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useYamlPreview = void 0;
const tslib_1 = require("tslib");
const cloneDeep_js_1 = tslib_1.__importDefault(require("lodash-es/cloneDeep.js"));
const React = tslib_1.__importStar(require("react"));
const yaml = tslib_1.__importStar(require("js-yaml"));
const resourceToString = (resource) => {
    var _a, _b, _c;
    if (!resource) {
        return undefined;
    }
    const resourceClone = (0, cloneDeep_js_1.default)(resource);
    // eslint-disable-next-line
    delete resourceClone.status;
    resourceClone.metadata = {
        name: (_a = resourceClone.metadata) === null || _a === void 0 ? void 0 : _a.name,
        namespace: (_b = resourceClone.metadata) === null || _b === void 0 ? void 0 : _b.namespace,
        labels: (_c = resourceClone.metadata) === null || _c === void 0 ? void 0 : _c.labels,
    };
    return yaml.dump(resourceClone);
};
const useYamlPreview = ({ clusterDeployment, agentClusterInstall, fetchSecret, fetchManagedClusters, fetchKlusterletAddonConfig, }) => {
    var _a, _b, _c, _d;
    const [loadingResources, setLoadingResources] = React.useState(true);
    const [pullSecret, setPullSecret] = React.useState();
    const [managedCluster, setManagedCluster] = React.useState();
    const [klusterlet, setKlusterlet] = React.useState();
    const cdName = (_a = clusterDeployment.metadata) === null || _a === void 0 ? void 0 : _a.name;
    const cdNamespace = (_b = clusterDeployment.metadata) === null || _b === void 0 ? void 0 : _b.namespace;
    const secretName = (_d = (_c = clusterDeployment.spec) === null || _c === void 0 ? void 0 : _c.pullSecretRef) === null || _d === void 0 ? void 0 : _d.name;
    React.useEffect(() => {
        const fetch = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
            const results = yield Promise.allSettled([
                fetchSecret(cdNamespace || '', secretName || ''),
                fetchManagedClusters(),
                fetchKlusterletAddonConfig(),
            ]);
            const [secretResult, managedClustersResult, klusterletsResult] = results;
            if (secretResult.status === 'fulfilled') {
                setPullSecret(secretResult.value);
            }
            if (managedClustersResult.status === 'fulfilled') {
                setManagedCluster(managedClustersResult.value.find((mc) => { var _a, _b; return ((_b = (_a = mc.metadata) === null || _a === void 0 ? void 0 : _a.labels) === null || _b === void 0 ? void 0 : _b.name) === cdName; }));
            }
            if (klusterletsResult.status === 'fulfilled') {
                setKlusterlet(klusterletsResult.value.find((kac) => 
                // eslint-disable-next-line
                kac.spec.clusterName === cdName &&
                    // eslint-disable-next-line
                    kac.spec.clusterNamespace === cdNamespace));
            }
            setLoadingResources(false);
        });
        void fetch();
    }, [
        fetchSecret,
        cdNamespace,
        cdName,
        secretName,
        fetchManagedClusters,
        fetchKlusterletAddonConfig,
    ]);
    const code = React.useMemo(() => [clusterDeployment, agentClusterInstall, pullSecret, managedCluster, klusterlet]
        .map(resourceToString)
        .filter((r) => !!r)
        .join(`\n---\n`), [clusterDeployment, agentClusterInstall, pullSecret, managedCluster, klusterlet]);
    return { code, loadingResources };
};
exports.useYamlPreview = useYamlPreview;
//# sourceMappingURL=useYAMLPreview.js.map