const findPath = (elem: HTMLElement, form: HTMLFormElement | null, path: string[] = []) => {
  let parent = elem.parentNode as ParentNode | null;

  while (parent && parent !== form) {
    if (parent.nodeName.toLowerCase() === 'fieldset') {
      const fieldset = parent as HTMLFieldSetElement;
      if (fieldset.name) {
        path.unshift(fieldset.name);
      }
    }
    parent = parent.parentNode;
  }
  return path;
};

export default findPath;
