"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.saveResourceBatch = exports.clearResourcesCache = exports.getResourcesByPathsWithCache = exports.getResourcesGroupSearchsWithCache = exports.getResourcesByPaths = exports.fetchResourceRemoteUrl = exports.updateGroupConfig = exports.deleteResourcesGroup = exports.addResourcesGroup = exports.fetchResourcesGroups = exports.getResourcesGroups = exports.putResourcesFolders = exports.postResourcesFolders = exports.putResourcesStatus = exports.putResourcesContents = exports.postResourcesContents = exports.fetchApplicationsResourcesGroupTypes = void 0;
const json_stable_stringify_1 = __importDefault(require("json-stable-stringify"));
const api_agent_1 = __importDefault(require("@/utils/api-agent"));
// getApplicationsResources
const fetchApplicationsResourcesGroupTypes = (params) => new Promise((resolve) => {
    api_agent_1.default.get('/applications/resources', params, (rs) => {
        resolve(rs);
    });
});
exports.fetchApplicationsResourcesGroupTypes = fetchApplicationsResourcesGroupTypes;
const postResourcesContents = (params) => new Promise((resolve) => {
    api_agent_1.default.post('/resources/contents', params, (rs) => {
        resolve(rs);
    });
});
exports.postResourcesContents = postResourcesContents;
const putResourcesContents = (params) => new Promise((resolve) => {
    api_agent_1.default.put('/resources/contents', params, (rs) => {
        resolve(rs);
    });
});
exports.putResourcesContents = putResourcesContents;
const putResourcesStatus = (params) => new Promise((resolve) => {
    api_agent_1.default.put('/resources/status', params, (rs) => {
        resolve(rs);
    });
});
exports.putResourcesStatus = putResourcesStatus;
const postResourcesFolders = (params) => new Promise((resolve) => {
    api_agent_1.default.post('/resources/folders', params, (rs) => {
        resolve(rs);
    });
});
exports.postResourcesFolders = postResourcesFolders;
const putResourcesFolders = (params) => new Promise((resolve) => {
    api_agent_1.default.put('/resources/folders', params, (rs) => {
        resolve(rs);
    });
});
exports.putResourcesFolders = putResourcesFolders;
const getResourcesGroups = (params) => new Promise((resolve) => {
    api_agent_1.default.get('/resources/groups', params, (rs) => {
        resolve(rs);
    });
});
exports.getResourcesGroups = getResourcesGroups;
// getResourcesGroupSearchs
const fetchResourcesGroups = (params) => new Promise((resolve) => {
    api_agent_1.default.get('/resources/group-searchs', params, (rs) => {
        resolve(rs);
    });
});
exports.fetchResourcesGroups = fetchResourcesGroups;
// postResourcesGroups
const addResourcesGroup = (params) => new Promise((resolve) => {
    api_agent_1.default.post('/resources/groups', params, (rs) => {
        resolve(rs);
    });
});
exports.addResourcesGroup = addResourcesGroup;
// putResourcesGroupStatus
const deleteResourcesGroup = (params) => new Promise((resolve) => {
    api_agent_1.default.put('/resources/group-status', params, (rs) => {
        resolve(rs);
    });
});
exports.deleteResourcesGroup = deleteResourcesGroup;
// editGroup
const updateGroupConfig = (params) => new Promise((resolve) => {
    api_agent_1.default.put('/resources/group', params, (rs) => {
        resolve(rs);
    });
});
exports.updateGroupConfig = updateGroupConfig;
const fetchResourceRemoteUrl = (params) => new Promise((resolve) => {
    api_agent_1.default.get('/resources/remote-url', params, (rs) => {
        resolve(rs);
    });
});
exports.fetchResourceRemoteUrl = fetchResourceRemoteUrl;
const getResourcesByPaths = (params) => new Promise((resolve) => {
    api_agent_1.default.get('/resources/by-paths', params, (rs) => {
        resolve(rs);
    });
});
exports.getResourcesByPaths = getResourcesByPaths;
let ApiCache = {};
const getResourcesGroupSearchsWithCache = (params) => {
    const cacheKey = `getResourcesGroupSearchs_${(0, json_stable_stringify_1.default)(params)}`;
    const { applicationId } = params;
    if (!ApiCache[cacheKey]) {
        ApiCache[cacheKey] = (0, exports.fetchResourcesGroups)({
            applicationId,
            size: 999,
        }).then((res) => {
            if (res.code !== 200 || !res.data) {
                console.error(res.msg);
                return [];
            }
            return res.data || [];
        });
    }
    return ApiCache[cacheKey];
};
exports.getResourcesGroupSearchsWithCache = getResourcesGroupSearchsWithCache;
const getResourcesByPathsWithCache = (params) => {
    const cacheKey = `getResourcesByPaths_${(0, json_stable_stringify_1.default)(params)}`;
    const { applicationId, path, depth = 1 } = params;
    if (!ApiCache[cacheKey]) {
        ApiCache[cacheKey] = (0, exports.getResourcesByPaths)({
            applicationId,
            path,
            depth,
        }).then((res) => {
            if (res.code !== 200) {
                console.error(res.msg);
                return {};
            }
            return res.data || {};
        });
    }
    return ApiCache[cacheKey];
};
exports.getResourcesByPathsWithCache = getResourcesByPathsWithCache;
const clearResourcesCache = () => {
    ApiCache = {};
};
exports.clearResourcesCache = clearResourcesCache;
const saveResourceBatch = (params) => new Promise((resolve) => {
    api_agent_1.default.post('/resources/batch', params, (rs) => {
        resolve(rs);
    });
});
exports.saveResourceBatch = saveResourceBatch;
