const VARIANT_APPLIES_TO = [
  "VTextField",
  "VSelect",
  "VAutocomplete",
  "VCombobox",
  "VTextarea",
  "VFileInput",
  "VCard",
  "FormDate",
  "FormTime",
  "FormDateTime",
  "MasterAutocomplete",
  "ModelAutocomplete",
  "ModelSelect",
  "ModelCombobox"
];
export const vuetify4MigrationRules = [
  { from: "dense", to: 'density="compact"' },
  { from: "outlined", to: 'variant="outlined"', appliesTo: VARIANT_APPLIES_TO },
  { from: "filled", to: 'variant="filled"', appliesTo: VARIANT_APPLIES_TO },
  { from: "solo", to: 'variant="solo"', appliesTo: VARIANT_APPLIES_TO },
  { from: "flat", to: 'variant="flat"', appliesTo: ["VCard"] }
];
export function migrateAttributeString(attrs, applicableRules) {
  if (!attrs || applicableRules.length === 0) return attrs;
  let result = "";
  let lastFlushed = 0;
  let i = 0;
  let inQuote = null;
  while (i < attrs.length) {
    const ch = attrs[i];
    if (inQuote) {
      if (ch === inQuote) inQuote = null;
      i++;
      continue;
    }
    if (ch === '"' || ch === "'") {
      inQuote = ch;
      i++;
      continue;
    }
    if (i === 0 || /\s/.test(attrs[i - 1])) {
      let matched = false;
      for (const rule of applicableRules) {
        const word = rule.from;
        if (attrs.slice(i, i + word.length) !== word) continue;
        const after = i + word.length;
        if (after !== attrs.length && !/\s/.test(attrs[after])) continue;
        result += attrs.slice(lastFlushed, i);
        result += rule.to;
        lastFlushed = after;
        i = after;
        matched = true;
        break;
      }
      if (matched) continue;
    }
    i++;
  }
  result += attrs.slice(lastFlushed);
  return result;
}
export function migrateInputAttributes(attrs, inputType, rules = vuetify4MigrationRules) {
  if (!attrs) return "";
  const applicable = rules.filter((r) => !r.appliesTo || r.appliesTo.includes(inputType));
  return migrateAttributeString(attrs, applicable);
}
export function migrateTemplateString(template, rules = vuetify4MigrationRules) {
  if (!template) return template;
  const applicable = rules.filter((r) => !r.appliesTo);
  if (applicable.length === 0) return template;
  let result = "";
  let i = 0;
  while (i < template.length) {
    const ch = template[i];
    if (ch !== "<") {
      result += ch;
      i++;
      continue;
    }
    if (template[i + 1] === "/" || template[i + 1] === "!") {
      result += ch;
      i++;
      continue;
    }
    const nameMatch = /^[a-z][\w:-]*/i.exec(template.slice(i + 1));
    if (!nameMatch) {
      result += ch;
      i++;
      continue;
    }
    const tagName = nameMatch[0];
    const attrsStart = i + 1 + tagName.length;
    let j = attrsStart;
    let inQuote = null;
    while (j < template.length) {
      const c = template[j];
      if (inQuote) {
        if (c === inQuote) inQuote = null;
      } else if (c === '"' || c === "'") {
        inQuote = c;
      } else if (c === ">") {
        break;
      }
      j++;
    }
    if (j >= template.length) {
      result += template.slice(i);
      break;
    }
    const tagEnd = template[j - 1] === "/" ? j - 1 : j;
    const attrs = template.slice(attrsStart, tagEnd);
    const closing = template.slice(tagEnd, j + 1);
    result += `<${tagName}${migrateAttributeString(attrs, applicable)}${closing}`;
    i = j + 1;
  }
  return result;
}
