"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.migrateFindDescriptor = migrateFindDescriptor;
exports.migrateVisualFind = migrateVisualFind;
exports.migrateCustomFind = migrateCustomFind;
exports.isMigratedCustomFindDescriptor = isMigratedCustomFindDescriptor;
const uuid_1 = require("uuid");
const mablscriptFind_1 = require("@mablhq/test-model/mablscriptFind");
function migrateFindDescriptor(descriptor) {
    if (isMigratedCustomFindDescriptor(descriptor)) {
        return descriptor;
    }
    if ((0, mablscriptFind_1.isFindCustomDescriptor)(descriptor)) {
        return migrateCustomFind(descriptor);
    }
    return migrateVisualFind(descriptor);
}
function migrateVisualFind(descriptor) {
    const primaryFindMethod = descriptor.findOptions?.primaryFindMethod;
    const findPrompt = descriptor.findTarget?.selector?.find_prompt;
    if (primaryFindMethod !== mablscriptFind_1.PrimaryFindMethod.ELEMENT_DESCRIPTION ||
        !findPrompt) {
        return descriptor;
    }
    const findOptions = { ...descriptor.findOptions };
    delete findOptions.primaryFindMethod;
    return {
        ...descriptor,
        findOptions,
    };
}
function migrateCustomFind(descriptor) {
    const legacyTarget = descriptor.findTarget;
    let locator;
    if ((0, mablscriptFind_1.isCssSelector)(legacyTarget)) {
        locator = legacyTarget.css_query;
    }
    else if ((0, mablscriptFind_1.isXPathSelector)(legacyTarget)) {
        locator = `xpath=${legacyTarget.xpath}`;
    }
    else {
        throw new Error(`Custom Find descriptor has unrecognized findTarget shape: ${JSON.stringify(legacyTarget)}`);
    }
    const uuid = pickUuid(descriptor);
    const migratedTarget = {
        selector: {
            uuid,
            locator,
        },
    };
    const findOptions = {
        ...descriptor.findOptions,
        autoHealOptions: {
            ...(descriptor.findOptions?.autoHealOptions ?? {}),
            isAutoHealAuthorized: false,
            useLegacyAutoheal: false,
        },
    };
    return {
        findType: descriptor.findType,
        findTarget: migratedTarget,
        ...(descriptor.foundElementSelectors !== undefined
            ? { foundElementSelectors: descriptor.foundElementSelectors }
            : {}),
        findOptions,
    };
}
function isMigratedCustomFindDescriptor(value) {
    if (!value || typeof value !== 'object') {
        return false;
    }
    const candidate = value;
    if (candidate.findType !== mablscriptFind_1.FindType.FIND_FIRST &&
        candidate.findType !== mablscriptFind_1.FindType.FIND_LAST &&
        candidate.findType !== mablscriptFind_1.FindType.FIND_ALL &&
        candidate.findType !== mablscriptFind_1.FindType.FIND_ANY) {
        return false;
    }
    if (!candidate.findTarget || typeof candidate.findTarget !== 'object') {
        return false;
    }
    const target = candidate.findTarget;
    if (!target.selector || typeof target.selector !== 'object') {
        return false;
    }
    const selector = target.selector;
    return typeof selector.locator === 'string' && selector.locator.length > 0;
}
function pickUuid(descriptor) {
    const fromTarget = descriptor.findTarget?.uuid;
    if (fromTarget) {
        return fromTarget;
    }
    const found = descriptor.foundElementSelectors;
    if (found) {
        if (Array.isArray(found)) {
            const targetUuid = found[0]?.uuid;
            if (targetUuid) {
                return targetUuid;
            }
        }
        else if (found.uuid) {
            return found.uuid;
        }
    }
    return (0, uuid_1.v4)();
}
