"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = (module)=>{
        var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    TaskCache: ()=>TaskCache,
    cacheFileExt: ()=>cacheFileExt,
    debug: ()=>debug
});
const external_node_assert_namespaceObject = require("node:assert");
var external_node_assert_default = /*#__PURE__*/ __webpack_require__.n(external_node_assert_namespaceObject);
const external_node_fs_namespaceObject = require("node:fs");
const external_node_path_namespaceObject = require("node:path");
const external_node_util_namespaceObject = require("node:util");
const common_namespaceObject = require("@midscene/shared/common");
const env_namespaceObject = require("@midscene/shared/env");
const logger_namespaceObject = require("@midscene/shared/logger");
const utils_namespaceObject = require("@midscene/shared/utils");
const external_js_yaml_namespaceObject = require("js-yaml");
var external_js_yaml_default = /*#__PURE__*/ __webpack_require__.n(external_js_yaml_namespaceObject);
const external_semver_namespaceObject = require("semver");
var external_semver_default = /*#__PURE__*/ __webpack_require__.n(external_semver_namespaceObject);
const external_utils_js_namespaceObject = require("./utils.js");
function _define_property(obj, key, value) {
    if (key in obj) Object.defineProperty(obj, key, {
        value: value,
        enumerable: true,
        configurable: true,
        writable: true
    });
    else obj[key] = value;
    return obj;
}
const DEFAULT_CACHE_MAX_FILENAME_LENGTH = 200;
const debug = (0, logger_namespaceObject.getDebug)('cache');
const lowestSupportedMidsceneVersion = '0.16.10';
const cacheFileExt = '.cache.yaml';
class TaskCache {
    matchCache(prompt, type) {
        if (!this.isCacheResultUsed) return;
        const promptStr = 'string' == typeof prompt ? prompt : JSON.stringify(prompt);
        for(let i = 0; i < this.cacheOriginalLength; i++){
            const item = this.cache.caches[i];
            const key = `${type}:${promptStr}:${i}`;
            if (item.type === type && (0, external_node_util_namespaceObject.isDeepStrictEqual)(item.prompt, prompt) && !this.matchedCacheIndices.has(key)) {
                if ('locate' === item.type) {
                    const locateItem = item;
                    if (!locateItem.cache && Array.isArray(locateItem.xpaths)) locateItem.cache = {
                        xpaths: locateItem.xpaths
                    };
                    if ('xpaths' in locateItem) locateItem.xpaths = void 0;
                }
                this.matchedCacheIndices.add(key);
                debug('cache found and marked as used, type: %s, prompt: %s, index: %d', type, prompt, i);
                return {
                    cacheContent: item,
                    updateFn: (cb)=>{
                        debug('will call updateFn to update cache, type: %s, prompt: %s, index: %d', type, prompt, i);
                        cb(item);
                        if (this.readOnlyMode) return void debug('read-only mode, cache updated in memory but not flushed to file');
                        debug('cache updated, will flush to file, type: %s, prompt: %s, index: %d', type, prompt, i);
                        this.flushCacheToFile();
                    }
                };
            }
        }
        debug('no unused cache found, type: %s, prompt: %s', type, prompt);
    }
    matchPlanCache(prompt) {
        return this.matchCache(prompt, 'plan');
    }
    matchLocateCache(prompt) {
        return this.matchCache(prompt, 'locate');
    }
    appendCache(cache) {
        debug('will append cache', cache);
        this.cache.caches.push(cache);
        if (this.readOnlyMode) return void debug('read-only mode, cache appended to memory but not flushed to file');
        this.flushCacheToFile();
    }
    loadCacheFromFile() {
        const cacheFile = this.cacheFilePath;
        external_node_assert_default()(cacheFile, 'cache file path is required');
        if (!(0, external_node_fs_namespaceObject.existsSync)(cacheFile)) return void debug('no cache file found, path: %s', cacheFile);
        const jsonTypeCacheFile = cacheFile.replace(cacheFileExt, '.json');
        if ((0, external_node_fs_namespaceObject.existsSync)(jsonTypeCacheFile) && this.isCacheResultUsed) return void console.warn(`An outdated cache file from an earlier version of Midscene has been detected. Since version 0.17, we have implemented an improved caching strategy. Please delete the old file located at: ${jsonTypeCacheFile}.`);
        try {
            const data = (0, external_node_fs_namespaceObject.readFileSync)(cacheFile, 'utf8');
            const jsonData = external_js_yaml_default().load(data);
            const version = (0, external_utils_js_namespaceObject.getMidsceneVersion)();
            if (!version) return void debug('no midscene version info, will not read cache from file');
            if (external_semver_default().lt(jsonData.midsceneVersion, lowestSupportedMidsceneVersion) && !jsonData.midsceneVersion.includes('beta')) return void console.warn(`You are using an old version of Midscene cache file, and we cannot match any info from it. Starting from Midscene v0.17, we changed our strategy to use xpath for cache info, providing better performance.\nPlease delete the existing cache and rebuild it. Sorry for the inconvenience.\ncache file: ${cacheFile}`);
            debug('cache loaded from file, path: %s, cache version: %s, record length: %s', cacheFile, jsonData.midsceneVersion, jsonData.caches.length);
            jsonData.midsceneVersion = (0, external_utils_js_namespaceObject.getMidsceneVersion)();
            return jsonData;
        } catch (err) {
            debug('cache file exists but load failed, path: %s, error: %s', cacheFile, err);
            return;
        }
    }
    flushCacheToFile(options) {
        const version = (0, external_utils_js_namespaceObject.getMidsceneVersion)();
        if (!version) return void debug('no midscene version info, will not write cache to file');
        if (!this.cacheFilePath) return void debug('no cache file path, will not write cache to file');
        if (options?.cleanUnused) if (this.isCacheResultUsed) {
            const originalLength = this.cache.caches.length;
            const usedIndices = new Set();
            for (const key of this.matchedCacheIndices){
                const parts = key.split(':');
                const index = Number.parseInt(parts[parts.length - 1], 10);
                if (!Number.isNaN(index)) usedIndices.add(index);
            }
            this.cache.caches = this.cache.caches.filter((_, index)=>{
                const isUsed = usedIndices.has(index);
                const isNew = index >= this.cacheOriginalLength;
                return isUsed || isNew;
            });
            const removedCount = originalLength - this.cache.caches.length;
            removedCount > 0 ? debug('cleaned %d unused cache record(s)', removedCount) : debug('no unused cache to clean');
        } else debug('skip cleaning: cache is not used for reading');
        try {
            const dir = (0, external_node_path_namespaceObject.dirname)(this.cacheFilePath);
            if (!(0, external_node_fs_namespaceObject.existsSync)(dir)) {
                (0, external_node_fs_namespaceObject.mkdirSync)(dir, {
                    recursive: true
                });
                debug('created cache directory: %s', dir);
            }
            const sortedCaches = [
                ...this.cache.caches
            ].sort((a, b)=>{
                if ('plan' === a.type && 'locate' === b.type) return -1;
                if ('locate' === a.type && 'plan' === b.type) return 1;
                return 0;
            });
            const cacheToWrite = {
                ...this.cache,
                caches: sortedCaches
            };
            const yamlData = external_js_yaml_default().dump(cacheToWrite, {
                lineWidth: -1
            });
            (0, external_node_fs_namespaceObject.writeFileSync)(this.cacheFilePath, yamlData);
            debug('cache flushed to file: %s', this.cacheFilePath);
        } catch (err) {
            debug('write cache to file failed, path: %s, error: %s', this.cacheFilePath, err);
        }
    }
    updateOrAppendCacheRecord(newRecord, cachedRecord) {
        if (cachedRecord) if ('plan' === newRecord.type) cachedRecord.updateFn((cache)=>{
            cache.yamlWorkflow = newRecord.yamlWorkflow;
        });
        else cachedRecord.updateFn((cache)=>{
            const locateCache = cache;
            locateCache.cache = newRecord.cache;
            if ('xpaths' in locateCache) locateCache.xpaths = void 0;
        });
        else this.appendCache(newRecord);
    }
    constructor(cacheId, isCacheResultUsed, cacheFilePath, options = {}){
        _define_property(this, "cacheId", void 0);
        _define_property(this, "cacheFilePath", void 0);
        _define_property(this, "cache", void 0);
        _define_property(this, "isCacheResultUsed", void 0);
        _define_property(this, "cacheOriginalLength", void 0);
        _define_property(this, "readOnlyMode", void 0);
        _define_property(this, "writeOnlyMode", void 0);
        _define_property(this, "matchedCacheIndices", new Set());
        external_node_assert_default()(cacheId, 'cacheId is required');
        let safeCacheId = (0, utils_namespaceObject.replaceIllegalPathCharsAndSpace)(cacheId);
        const cacheMaxFilenameLength = env_namespaceObject.globalConfigManager.getEnvConfigValueAsNumber(env_namespaceObject.MIDSCENE_CACHE_MAX_FILENAME_LENGTH) ?? DEFAULT_CACHE_MAX_FILENAME_LENGTH;
        if (Buffer.byteLength(safeCacheId, 'utf8') > cacheMaxFilenameLength) {
            const prefix = safeCacheId.slice(0, 32);
            const hash = (0, utils_namespaceObject.generateHashId)(void 0, safeCacheId);
            safeCacheId = `${prefix}-${hash}`;
        }
        this.cacheId = safeCacheId;
        this.cacheFilePath = utils_namespaceObject.ifInBrowser || utils_namespaceObject.ifInWorker ? void 0 : cacheFilePath || (0, external_node_path_namespaceObject.join)((0, common_namespaceObject.getMidsceneRunSubDir)('cache'), `${this.cacheId}${cacheFileExt}`);
        const readOnlyMode = Boolean(options?.readOnly);
        const writeOnlyMode = Boolean(options?.writeOnly);
        if (readOnlyMode && writeOnlyMode) throw new Error('TaskCache cannot be both read-only and write-only');
        this.isCacheResultUsed = writeOnlyMode ? false : isCacheResultUsed;
        this.readOnlyMode = readOnlyMode;
        this.writeOnlyMode = writeOnlyMode;
        let cacheContent;
        if (this.cacheFilePath && !this.writeOnlyMode) cacheContent = this.loadCacheFromFile();
        if (!cacheContent) cacheContent = {
            midsceneVersion: (0, external_utils_js_namespaceObject.getMidsceneVersion)(),
            cacheId: this.cacheId,
            caches: []
        };
        this.cache = cacheContent;
        this.cacheOriginalLength = this.isCacheResultUsed ? this.cache.caches.length : 0;
    }
}
exports.TaskCache = __webpack_exports__.TaskCache;
exports.cacheFileExt = __webpack_exports__.cacheFileExt;
exports.debug = __webpack_exports__.debug;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "TaskCache",
    "cacheFileExt",
    "debug"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=task-cache.js.map