/**
 * XPath utility functions for CSS to XPath conversion
 * @module xpath-utils
 */

'use strict';

/**
 * Creates an XPath expression to convert a string to lowercase
 * @param {string} str - String expression to convert to lowercase
 * @returns {string} XPath expression for lowercase conversion
 */
export function convertToLowercase(str) {
  return 'translate(' +
    (str || 'normalize-space()') +
    ', \'ABCDEFGHJIKLMNOPQRSTUVWXYZ\'' +
    ', \'abcdefghjiklmnopqrstuvwxyz\')';
}

/**
 * Creates an XPath ends-with expression (XPath 1.0 compatible)
 * @param {string} str1 - The string to check
 * @param {string} str2 - The substring to check for at the end
 * @returns {string} XPath expression for ends-with
 */
export function createEndsWith(str1, str2) {
  return 'substring(' + str1 + ',' +
    'string-length(' + str1 + ')-string-length(' + str2 + ')+1)=' + str2;
}

/**
 * Creates an XPath expression to extract URL from an attribute
 * @param {string} str - The attribute to extract URL from
 * @param {string} [urlAttrs='@href|@src'] - URL attributes to check
 * @returns {string} XPath expression for URL extraction
 */
export function extractUrl(str, urlAttrs = '@href|@src') {
  return 'substring-before(concat(substring-after(' +
    (str || urlAttrs) + ',"://"),"?"),"?")';
}

/**
 * Creates an XPath expression to extract URL path
 * @param {string} str - The attribute to extract URL path from
 * @param {string} [urlAttrs='@href|@src'] - URL attributes to check
 * @returns {string} XPath expression for URL path extraction
 */
export function extractUrlPath(str, urlAttrs = '@href|@src') {
  return 'substring-after(' + (str || urlAttrs) + ',"/")';
}

/**
 * Creates an XPath expression to extract URL domain
 * @param {string} str - The attribute to extract URL domain from
 * @param {string} [urlAttrs='@href|@src'] - URL attributes to check
 * @returns {string} XPath expression for URL domain extraction
 */
export function extractUrlDomain(str, urlAttrs = '@href|@src') {
  return 'substring-before(concat(substring-after(' +
    (str || urlAttrs) + ',"://"),"/"),"/")';
}

/**
 * Validates if a string is a valid CSS selector
 * @param {any} selector - The selector to validate
 * @returns {boolean} True if valid
 */
export function validateSelector(selector) {
  if (typeof selector !== 'string' && selector !== null && selector !== undefined) {
    throw new TypeError('CSS selector must be a string, null, or undefined');
  }
  
  try {
    // Simple validation - attempt to use the selector with document.querySelector
    // This would run only in browser environments
    if (typeof document !== 'undefined') {
      document.querySelector(selector);
    }
    return true;
  } catch (e) {
    return false;
  }
}

/**
 * Generates a repeated string efficiently
 * @param {string} str - The string to repeat
 * @param {number} num - Number of times to repeat
 * @returns {string} The repeated string
 */
export function generateRepeatedString(str, num) {
  num = Number(num);
  let result = '';

  while (true) {
    if (num & 1) {
      result += str;
    }
    num >>>= 1;

    if (num <= 0) {
      break;
    }
    str += str;
  }

  return result;
}

/**
 * Checks if a string is a numeric value
 * @param {string} s - String to check
 * @returns {boolean} True if numeric
 */
export function isNumericValue(s) {
  const num = parseInt(s, 10);
  return (!isNaN(num) && '' + num === s);
}

/**
 * Creates a formatted XPath expression using template literals
 * @param {string} tagName - Element tag name
 * @param {string} attr - Attribute name
 * @param {string} value - Attribute value
 * @returns {string} Formatted XPath expression
 */
export function createXPathExpression(tagName, attr, value) {
  return `.//${tagName}[@${attr}="${value}"]`;
} 