"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExtractAction = exports.ExtractTypeName = exports.CSS_EXTRACTION_VALUE = exports.EXTRACTION_TYPE_KEY = void 0;
const MablAction_1 = require("../MablAction");
const ExtractDescriptor_1 = require("../types/ExtractDescriptor");
exports.EXTRACTION_TYPE_KEY = 'extraction_type';
exports.CSS_EXTRACTION_VALUE = 'css';
var ExtractTypeName;
(function (ExtractTypeName) {
    ExtractTypeName["CssProperty"] = "CssProperty";
    ExtractTypeName["ElementAttribute"] = "ElementAttribute";
    ExtractTypeName["EmailAttribute"] = "EmailAttribute";
})(ExtractTypeName || (exports.ExtractTypeName = ExtractTypeName = {}));
class ExtractAction extends MablAction_1.MablAction {
    static mablScriptStepNames = [
        'extract_attribute',
        'extract_email_attribute',
        'extract_inner_text',
    ];
    static mablscriptToYamlType = {
        extract_attribute: ExtractTypeName.ElementAttribute,
        extract_css_property: ExtractTypeName.CssProperty,
        extract_email_attribute: ExtractTypeName.EmailAttribute,
    };
    extractionAttribute;
    extractionType;
    constructor(name, args) {
        super(name, name, args);
        if (this.name === ExtractDescriptor_1.ExtractType.EXTRACT_INNER_TEXT) {
            this.name = ExtractDescriptor_1.ExtractType.EXTRACT_ATTRIBUTE;
            this.extractionAttribute = 'innerText';
        }
        else {
            this.extractionAttribute = this.getActionArgs()[0];
        }
        const actionArgs = this.getActionArgs();
        if (this.name === ExtractDescriptor_1.ExtractType.EXTRACT_ATTRIBUTE && actionArgs.length > 1) {
            const extractionOptions = actionArgs[1];
            const metaExtractionType = extractionOptions.extraction_type;
            if (metaExtractionType?.toLowerCase() === exports.CSS_EXTRACTION_VALUE) {
                this.name = ExtractDescriptor_1.ExtractType.EXTRACT_CSS_PROPERTIES;
            }
        }
        this.extractionType = ExtractDescriptor_1.ExtractType.fromString(this.name);
        if (!this.extractionAttribute) {
            throw new Error(`Attribute name is required for extract attribute action.`);
        }
        if (typeof this.extractionAttribute !== 'string') {
            throw new Error(`Invalid attribute provided for extract attribute extractor: ${this.extractionAttribute}`);
        }
    }
    toDescriptor() {
        return {
            extractType: this.extractionType,
            attributeName: this.extractionAttribute,
        };
    }
    toMablscript() {
        if (this.extractionType === ExtractDescriptor_1.ExtractType.EXTRACT_CSS_PROPERTIES) {
            return `extract_attribute("${this.extractionAttribute}", {${exports.EXTRACTION_TYPE_KEY}: "${exports.CSS_EXTRACTION_VALUE}"})`;
        }
        return `${this.extractionType}("${this.extractionAttribute}")`;
    }
    static mablScriptTypeFromYamlType(yamlType) {
        return (Object.keys(ExtractAction.mablscriptToYamlType).find((key) => ExtractAction.mablscriptToYamlType[key] === yamlType) || 'extract_attribute');
    }
}
exports.ExtractAction = ExtractAction;
