# Path: src/formpack/utils/flatten_content.py
# def flatten_content(survey_content, in_place=False, **opts):
#     if in_place:
#         flatten_content_in_place(survey_content, **opts)
#         return None
#     else:
#         survey_content_copy = deepcopy(survey_content)
#         flatten_content_in_place(survey_content_copy, **opts)
#         return survey_content_copy


import re
from pyquery import PyQuery
from pyxform.builder import create_survey_element_from_dict
from pyxform.xls2json import workbook_to_json
from .flatten_content import flatten_content
)


def formversion_pyxform(data):
    content = flatten_content(data)
    imported_survey_json = workbook_to_json(content)
    return create_survey_element_from_dict(imported_survey_json)


def get_version_identifiers(node):
    pqi = PyQuery(node)
    return pqi.attr('id_string'), pqi.attr('version')


def parse_xml_to_xmljson(node):
    pqi = PyQuery(node)
    items = pqi[0].attrib
    out = {'tag': pqi[0].tag}
    if len(items) > 0:
        out['attributes'] = dict(items)
    if len(pqi.children()) > 0:
        out['children'] = []
        for child in pqi.children():
            out['children'].append(parse_xml_to_xmljson(child))
    else:
        out['text'] = pqi.text()
    return out


def parse_xmljson_to_data(data, parent_tags=[], output=[]):

# Path: src/formpack/utils/flatten_content.py
# def flatten_content(survey_content, in_place=False, **opts):
#     if in_place:
#         flatten_content_in_place(survey_content, **opts)
#         return None
#     else:
#         survey_content_copy = deepcopy(survey_content)
#         flatten_content_in_place(survey_content_copy, **opts)
#         return survey_content_copy


# Path: src/formpack/utils/xform_tools.py
import re
from pyquery import PyQuery
from pyxform.builder import create_survey_element_from_dict
from pyxform.xls2json import workbook_to_json
from .flatten_content import flatten_content

)


def formversion_pyxform(data):
    content = flatten_content(data)
    imported_survey_json = workbook_to_json(content)
    return create_survey_element_from_dict(imported_survey_json)


def get_version_identifiers(node):
    pqi = PyQuery(node)
    return pqi.attr('id_string'), pqi.attr('version')


def parse_xml_to_xmljson(node):
    pqi = PyQuery(node)
    items = pqi[0].attrib
    out = {'tag': pqi[0].tag}
    if len(items) > 0:
        out['attributes'] = dict(items)
    if len(pqi.children()) > 0:
        out['children'] = []
        for child in pqi.children():
            out['children'].append(parse_xml_to_xmljson(child))
    else:
        out['text'] = pqi.text()
    return out


def parse_xmljson_to_data(data, parent_tags=[], output=[]):

    tag = data.get('tag')