"use strict";
// Types for direct style manipulation with immediate visual feedback
// This enables users to modify styles in real-time and translate to code on publish
Object.defineProperty(exports, "__esModule", { value: true });
exports.StyleApplicationError = exports.ElementNotFoundError = exports.DOMError = exports.changeKeyFromChange = exports.matchesElementIdentity = exports.getElementIdentityKey = exports.getElementIdentityCandidates = exports.textChangeKey = exports.changeKey = exports.Err = exports.Ok = void 0;
/**
 * Helper constructors for Result type
 */
const Ok = (value) => ({ ok: true, value });
exports.Ok = Ok;
const Err = (error) => ({ ok: false, error });
exports.Err = Err;
/**
 * Creates a unique key for a style change (element + property)
 */
const changeKey = (elementId, property) => `style:${elementId}:${property}`;
exports.changeKey = changeKey;
/**
 * Creates a unique key for a text change (element only - one text change per element)
 */
const textChangeKey = (elementId) => `text:${elementId}`;
exports.textChangeKey = textChangeKey;
/**
 * Returns normalized identifiers used to match a tracked element.
 */
const getElementIdentityCandidates = (elementId) => {
    const candidates = [elementId.rivetId, elementId.xpath, elementId.cssSelector]
        .filter((value) => Boolean(value))
        .map((value) => value.trim())
        .filter(Boolean);
    return candidates.length > 0 ? [...new Set(candidates)] : ['unknown'];
};
exports.getElementIdentityCandidates = getElementIdentityCandidates;
/**
 * Returns the primary identity key used for map keys.
 */
const getElementIdentityKey = (elementId) => {
    return (0, exports.getElementIdentityCandidates)(elementId)[0];
};
exports.getElementIdentityKey = getElementIdentityKey;
/**
 * Checks whether an element identifier matches a candidate key.
 */
const matchesElementIdentity = (elementId, candidateKey) => {
    return (0, exports.getElementIdentityCandidates)(elementId).includes(candidateKey);
};
exports.matchesElementIdentity = matchesElementIdentity;
/**
 * Creates a unique key from a TrackedChange object
 */
const changeKeyFromChange = (change) => {
    const elementId = (0, exports.getElementIdentityKey)(change.elementId);
    const batchSuffix = change.batchId ? `:${change.batchId}` : '';
    if (change.type === 'style') {
        return `${(0, exports.changeKey)(elementId, change.property)}${batchSuffix}`;
    }
    else {
        return `${(0, exports.textChangeKey)(elementId)}${batchSuffix}`;
    }
};
exports.changeKeyFromChange = changeKeyFromChange;
/**
 * Error types for DOM manipulation operations
 */
class DOMError extends Error {
    constructor(message) {
        super(message);
        this.name = 'DOMError';
    }
}
exports.DOMError = DOMError;
class ElementNotFoundError extends DOMError {
    identifier;
    constructor(identifier, message) {
        super(message ||
            `Element not found: rivetId=${identifier.rivetId}, xpath=${identifier.xpath}`);
        this.identifier = identifier;
        this.name = 'ElementNotFoundError';
    }
}
exports.ElementNotFoundError = ElementNotFoundError;
class StyleApplicationError extends DOMError {
    property;
    value;
    constructor(property, value, message) {
        super(message ||
            `Failed to apply style: ${property} = ${value}. Value may be invalid.`);
        this.property = property;
        this.value = value;
        this.name = 'StyleApplicationError';
    }
}
exports.StyleApplicationError = StyleApplicationError;
//# sourceMappingURL=style-manipulation-types.js.map