"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__, {
    getCurrentExecutionFile: ()=>getCurrentExecutionFile,
    getMidsceneVersion: ()=>getMidsceneVersion,
    getReportFileName: ()=>getReportFileName,
    matchElementFromCache: ()=>matchElementFromCache,
    matchElementFromPlan: ()=>matchElementFromPlan,
    commonContextParser: ()=>commonContextParser,
    parsePrompt: ()=>parsePrompt,
    transformLogicalElementToScreenshot: ()=>transformLogicalElementToScreenshot,
    generateCacheId: ()=>generateCacheId,
    ifPlanLocateParamIsBbox: ()=>ifPlanLocateParamIsBbox,
    printReportMsg: ()=>printReportMsg,
    transformLogicalRectToScreenshotRect: ()=>transformLogicalRectToScreenshotRect
});
const external_screenshot_item_js_namespaceObject = require("../screenshot-item.js");
const external_utils_js_namespaceObject = require("../utils.js");
const env_namespaceObject = require("@midscene/shared/env");
const extractor_namespaceObject = require("@midscene/shared/extractor");
const img_namespaceObject = require("@midscene/shared/img");
const logger_namespaceObject = require("@midscene/shared/logger");
const utils_namespaceObject = require("@midscene/shared/utils");
const external_dayjs_namespaceObject = require("dayjs");
var external_dayjs_default = /*#__PURE__*/ __webpack_require__.n(external_dayjs_namespaceObject);
const external_task_cache_js_namespaceObject = require("./task-cache.js");
async function commonContextParser(interfaceInstance, _opt) {
    const debug = (0, logger_namespaceObject.getDebug)('commonContextParser');
    (0, utils_namespaceObject.assert)(interfaceInstance, 'interfaceInstance is required');
    debug("Getting interface description");
    const description = interfaceInstance.describe?.() || '';
    debug("Interface description end");
    debug('Uploading test info to server');
    (0, external_utils_js_namespaceObject.uploadTestInfoToServer)({
        testUrl: description,
        serverUrl: _opt.uploadServerUrl
    });
    debug('UploadTestInfoToServer end');
    debug('will get size');
    const interfaceSize = await interfaceInstance.size();
    const { width: logicalWidth, height: logicalHeight } = interfaceSize;
    if (interfaceSize.dpr) console.warn('Warning: return value of interface.size() include a dpr property, which is not expected and ignored. ');
    if (!Number.isFinite(logicalWidth) || !Number.isFinite(logicalHeight)) throw new Error(`Invalid interface size: width and height must be finite numbers. Received width: ${logicalWidth}, height: ${logicalHeight}`);
    if (logicalWidth <= 0 || logicalHeight <= 0) throw new Error(`Invalid interface size: width and height must be positive numbers. Received width: ${logicalWidth}, height: ${logicalHeight}`);
    debug(`size: ${logicalWidth}x${logicalHeight}`);
    const screenshotBase64 = await interfaceInstance.screenshotBase64();
    const screenshotCapturedAt = Date.now();
    (0, utils_namespaceObject.assert)(screenshotBase64, 'screenshotBase64 is required');
    debug('will get screenshot dimensions');
    const { width: imgWidth, height: imgHeight } = await (0, img_namespaceObject.imageInfoOfBase64)(screenshotBase64);
    if (!Number.isFinite(imgWidth) || !Number.isFinite(imgHeight)) throw new Error(`Invalid screenshot dimensions: width and height must be finite numbers. Received width: ${imgWidth}, height: ${imgHeight}`);
    if (imgWidth <= 0 || imgHeight <= 0) throw new Error(`Invalid screenshot dimensions: width and height must be positive numbers. Received width: ${imgWidth}, height: ${imgHeight}`);
    debug('screenshot dimensions', imgWidth, 'x', imgHeight);
    const logicalIsPortrait = logicalWidth < logicalHeight;
    const screenshotIsPortrait = imgWidth < imgHeight;
    let finalLogicalWidth = logicalWidth;
    if (logicalIsPortrait !== screenshotIsPortrait) {
        debug(`Orientation mismatch detected: logical size ${logicalWidth}x${logicalHeight} (${logicalIsPortrait ? 'portrait' : 'landscape'}) vs screenshot ${imgWidth}x${imgHeight} (${screenshotIsPortrait ? 'portrait' : 'landscape'}). Swapping logical dimensions.`);
        finalLogicalWidth = logicalHeight;
    }
    const userShrinkFactor = _opt.screenshotShrinkFactor ?? 1;
    if (!Number.isFinite(userShrinkFactor) || userShrinkFactor < 1) throw new Error(`Invalid screenshotShrinkFactor: must be a finite number >= 1. Received: ${userShrinkFactor}`);
    const dpr = imgWidth / finalLogicalWidth;
    debug('calculated dpr:', dpr);
    const shrunkShotToLogicalRatio = dpr / userShrinkFactor;
    debug('shrunkShotToLogicalRatio', shrunkShotToLogicalRatio);
    if (1 !== userShrinkFactor) {
        const targetWidth = Math.round(imgWidth / userShrinkFactor);
        const targetHeight = Math.round(imgHeight / userShrinkFactor);
        debug(`Applying screenshot shrink factor: ${userShrinkFactor} (physical: ${imgWidth}x${imgHeight} -> target: ${targetWidth}x${targetHeight})`);
        const resizedBase64 = await (0, img_namespaceObject.resizeImgBase64)(screenshotBase64, {
            width: targetWidth,
            height: targetHeight
        });
        return {
            shotSize: {
                width: targetWidth,
                height: targetHeight
            },
            deprecatedDpr: dpr,
            screenshot: external_screenshot_item_js_namespaceObject.ScreenshotItem.create(resizedBase64, screenshotCapturedAt),
            shrunkShotToLogicalRatio
        };
    }
    return {
        shotSize: {
            width: imgWidth,
            height: imgHeight
        },
        deprecatedDpr: dpr,
        screenshot: external_screenshot_item_js_namespaceObject.ScreenshotItem.create(screenshotBase64, screenshotCapturedAt),
        shrunkShotToLogicalRatio
    };
}
function getReportFileName(tag = 'web') {
    const reportTagName = env_namespaceObject.globalConfigManager.getEnvConfigValue(env_namespaceObject.MIDSCENE_REPORT_TAG_NAME);
    const dateTimeInFileName = external_dayjs_default()().format('YYYY-MM-DD_HH-mm-ss');
    const uniqueId = (0, utils_namespaceObject.uuid)().substring(0, 8);
    return `${reportTagName || tag}-${dateTimeInFileName}-${uniqueId}`;
}
function printReportMsg(filepath) {
    if (env_namespaceObject.globalConfigManager.getEnvConfigInBoolean(env_namespaceObject.MIDSCENE_REPORT_QUIET)) return;
    (0, utils_namespaceObject.logMsg)(`Midscene - report file updated: ${filepath}`);
}
function getCurrentExecutionFile(trace) {
    const error = new Error();
    const stackTrace = trace || error.stack;
    const pkgDir = process.cwd() || '';
    if (stackTrace) {
        const stackLines = stackTrace.split('\n');
        for (const line of stackLines)if (line.includes('.spec.') || line.includes('.test.') || line.includes('.ts') || line.includes('.js')) {
            const match = line.match(/(?:at\s+)?(.*?\.(?:spec|test)\.[jt]s)/);
            if (match?.[1]) {
                const targetFileName = match[1].replace(pkgDir, '').trim().replace('at ', '');
                return targetFileName;
            }
        }
    }
    return false;
}
const testFileIndex = new Map();
function generateCacheId(fileName) {
    let taskFile = fileName || getCurrentExecutionFile();
    if (!taskFile) {
        taskFile = (0, utils_namespaceObject.uuid)();
        console.warn('Midscene - using random UUID for cache id. Cache may be invalid.');
    }
    if (testFileIndex.has(taskFile)) {
        const currentIndex = testFileIndex.get(taskFile);
        if (void 0 !== currentIndex) testFileIndex.set(taskFile, currentIndex + 1);
    } else testFileIndex.set(taskFile, 1);
    return `${taskFile}-${testFileIndex.get(taskFile)}`;
}
function ifPlanLocateParamIsBbox(planLocateParam) {
    return !!(planLocateParam.bbox && Array.isArray(planLocateParam.bbox) && 4 === planLocateParam.bbox.length);
}
function matchElementFromPlan(planLocateParam) {
    if (!planLocateParam) return;
    if (planLocateParam.bbox) {
        const rect = {
            left: planLocateParam.bbox[0],
            top: planLocateParam.bbox[1],
            width: planLocateParam.bbox[2] - planLocateParam.bbox[0] + 1,
            height: planLocateParam.bbox[3] - planLocateParam.bbox[1] + 1
        };
        const element = (0, extractor_namespaceObject.generateElementByRect)(rect, 'string' == typeof planLocateParam.prompt ? planLocateParam.prompt : planLocateParam.prompt?.prompt || '');
        return element;
    }
}
async function matchElementFromCache(context, cacheEntry, cachePrompt, cacheable) {
    if (!cacheEntry) return;
    if (false === cacheable) return void (0, external_task_cache_js_namespaceObject.debug)('cache disabled for prompt: %s', cachePrompt);
    if (!context.taskCache?.isCacheResultUsed) return;
    if (!context.interfaceInstance.rectMatchesCacheFeature) return void (0, external_task_cache_js_namespaceObject.debug)('interface does not implement rectMatchesCacheFeature, skip cache');
    try {
        const rect = await context.interfaceInstance.rectMatchesCacheFeature(cacheEntry);
        const element = {
            center: [
                Math.round(rect.left + rect.width / 2),
                Math.round(rect.top + rect.height / 2)
            ],
            rect,
            description: 'string' == typeof cachePrompt ? cachePrompt : cachePrompt.prompt || ''
        };
        (0, external_task_cache_js_namespaceObject.debug)('cache hit, prompt: %s', cachePrompt);
        return element;
    } catch (error) {
        (0, external_task_cache_js_namespaceObject.debug)('rectMatchesCacheFeature error: %s', error);
        return;
    }
}
const getMidsceneVersion = ()=>"1.5.7-beta-20260318053429.0";
const parsePrompt = (prompt)=>{
    if ('string' == typeof prompt) return {
        textPrompt: prompt,
        multimodalPrompt: void 0
    };
    return {
        textPrompt: prompt.prompt,
        multimodalPrompt: prompt.images ? {
            images: prompt.images,
            convertHttpImage2Base64: !!prompt.convertHttpImage2Base64
        } : void 0
    };
};
const transformLogicalElementToScreenshot = (element, shrunkShotToLogicalRatio)=>{
    if (1 === shrunkShotToLogicalRatio) return element;
    return {
        ...element,
        center: [
            Math.round(element.center[0] * shrunkShotToLogicalRatio),
            Math.round(element.center[1] * shrunkShotToLogicalRatio)
        ],
        rect: {
            ...element.rect,
            left: Math.round(element.rect.left * shrunkShotToLogicalRatio),
            top: Math.round(element.rect.top * shrunkShotToLogicalRatio),
            width: Math.round(element.rect.width * shrunkShotToLogicalRatio),
            height: Math.round(element.rect.height * shrunkShotToLogicalRatio)
        }
    };
};
const transformLogicalRectToScreenshotRect = (rect, shrunkShotToLogicalRatio)=>{
    if (1 === shrunkShotToLogicalRatio) return rect;
    return {
        ...rect,
        left: Math.round(rect.left * shrunkShotToLogicalRatio),
        top: Math.round(rect.top * shrunkShotToLogicalRatio),
        width: Math.round(rect.width * shrunkShotToLogicalRatio),
        height: Math.round(rect.height * shrunkShotToLogicalRatio)
    };
};
exports.commonContextParser = __webpack_exports__.commonContextParser;
exports.generateCacheId = __webpack_exports__.generateCacheId;
exports.getCurrentExecutionFile = __webpack_exports__.getCurrentExecutionFile;
exports.getMidsceneVersion = __webpack_exports__.getMidsceneVersion;
exports.getReportFileName = __webpack_exports__.getReportFileName;
exports.ifPlanLocateParamIsBbox = __webpack_exports__.ifPlanLocateParamIsBbox;
exports.matchElementFromCache = __webpack_exports__.matchElementFromCache;
exports.matchElementFromPlan = __webpack_exports__.matchElementFromPlan;
exports.parsePrompt = __webpack_exports__.parsePrompt;
exports.printReportMsg = __webpack_exports__.printReportMsg;
exports.transformLogicalElementToScreenshot = __webpack_exports__.transformLogicalElementToScreenshot;
exports.transformLogicalRectToScreenshotRect = __webpack_exports__.transformLogicalRectToScreenshotRect;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "commonContextParser",
    "generateCacheId",
    "getCurrentExecutionFile",
    "getMidsceneVersion",
    "getReportFileName",
    "ifPlanLocateParamIsBbox",
    "matchElementFromCache",
    "matchElementFromPlan",
    "parsePrompt",
    "printReportMsg",
    "transformLogicalElementToScreenshot",
    "transformLogicalRectToScreenshotRect"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});

//# sourceMappingURL=utils.js.map