import frappe

SKIP_FIELDS = {"name", "creation", "modified", "modified_by", "owner", "docstatus", "idx"}
CHILD_SKIP_FIELDS = {"name", "creation", "modified", "modified_by", "owner",
                     "docstatus", "idx", "parent", "parenttype", "parentfield", "company"}


@frappe.whitelist()
def export_salary_components():
    components = frappe.get_all("Salary Component", fields=["name"])
    result = []

    for c in components:
        doc = frappe.get_doc("Salary Component", c.name)
        data = doc.as_dict()

        for field in SKIP_FIELDS:
            data.pop(field, None)

        # В accounts сохраняем account_name вместо полного имени (без суффикса компании)
        accounts = []
        for row in data.get("accounts") or []:
            if row.get("account"):
                account_name = frappe.db.get_value("Account", row["account"], "account_name")
                row["account"] = account_name
            for field in CHILD_SKIP_FIELDS:
                row.pop(field, None)
            accounts.append(row)
        data["accounts"] = accounts

        result.append(data)

    return result
