import numpy as np

from .._helpers import _writer_map, read, reader_map, write


def add_args(parser):
    parser.add_argument("infile", type=str, help="mesh file to be read from")
    parser.add_argument(
        "--input-format",
        "-i",
        type=str,
        choices=sorted(list(reader_map.keys())),
        help="input file format",
        default=None,
    )
    parser.add_argument(
        "--output-format",
        "-o",
        type=str,
        choices=sorted(list(_writer_map.keys())),
        help="output file format",
        default=None,
    )
    parser.add_argument(
        "--ascii",
        "-a",
        action="store_true",
        help="write in ASCII format variant (where applicable, default: binary)",
    )
    parser.add_argument("outfile", type=str, help="mesh file to be written to")
    parser.add_argument(
        "--float-format",
        "-f",
        type=str,
        help="float format used in output ASCII files (default: .16e)",
    )
    parser.add_argument(
        "--prune",
        "-p",
        action="store_true",
        help="remove lower order cells, remove orphaned nodes",
    )
    parser.add_argument(
        "--prune-z-0",
        "-z",
        action="store_true",
        help="remove third (z) dimension if all points are 0",
    )
    parser.add_argument(
        "--sets-to-int-data",
        "-s",
        action="store_true",
        help="if possible, convert sets to integer data (useful if the output type does not support sets)",
    )
    parser.add_argument(
        "--int-data-to-sets",
        "-d",
        action="store_true",
        help="if possible, convert integer data to sets (useful if the output type does not support integer data)",
    )


def convert(args):
    # read mesh data
    mesh = read(args.infile, file_format=args.input_format)

    if args.prune:
        mesh.prune()

    if (
        args.prune_z_0
        and mesh.points.shape[1] == 3
        and np.all(np.abs(mesh.points[:, 2]) < 1.0e-13)
    ):
        mesh.points = mesh.points[:, :2]

    # Some converters (like VTK) require `points` to be contiguous.
    mesh.points = np.ascontiguousarray(mesh.points)

    if args.sets_to_int_data:
        mesh.sets_to_int_data()

    if args.int_data_to_sets:
        mesh.int_data_to_sets()

    # write it out
    kwargs = {"file_format": args.output_format}
    if args.float_format is not None:
        kwargs["float_fmt"] = args.float_format
    if args.ascii:
        kwargs["binary"] = False

    write(args.outfile, mesh, **kwargs)

@@ -1011,325 +1011,8 @@
 nt(%0A
-        %22--prune%22,%0A        %22-p%22,%0A        action=%22store_true%22,%0A        help=%22remove lower order cells, remove orphaned nodes%22,%0A    )%0A    parser.add_argument(%0A        %22--prune-z-0%22,%0A        %22-z%22,%0A        action=%22store_true%22,%0A        help=%22remove third (z) dimension if all points are 0%22,%0A    )%0A    parser.add_argument(%0A
     
@@ -1521,224 +1521,8 @@
 t)%0A%0A
-    if args.prune:%0A        mesh.prune()%0A%0A    if (%0A        args.prune_z_0%0A        and mesh.points.shape%5B1%5D == 3%0A        and np.all(np.abs(mesh.points%5B:, 2%5D) %3C 1.0e-13)%0A    ):%0A        mesh.points = mesh.points%5B:, :2%5D%0A%0A
     

remove leftover prunes