from typing import OrderedDict


def get_star_ids_from_upload_file(fullpath):
    """  Get and return all star ids from a given WebObs upload text file.
    :param fullpath: upload text file name [string].
    :return: list of all star IDs, no duplicates, preserving order found in file [list of strings].
    """
    try:
        with open(fullpath) as f:
            lines = f.readlines()
    except FileNotFoundError:
        return list()

    # Ensure file declares itself to be either Extended or Visual format:
    type_line_found = False
    for line in lines:
        if line.upper().startswith('#TYPE='):
            type_line_found = True
            type_string = line.split('=')[1].strip()
            if type_string.upper() not in ['EXTENDED', 'VISUAL']:
                return list()
    if not type_line_found:
        return list()

    # Get delimiter, comma as default:
    delimiter = ','  # default if #DELIM line not found.
    for line in lines:
        if line.upper().startswith('#DELIM='):
            delimiter = line.split('=')[1].strip()
            if delimiter.lower() == 'comma':
                delimiter = ','
            break

    # Extract and return star_ids:
    lines = [line for line in lines if not line.startswith('#')]  # keep only observation text lines.
    star_ids_as_found = [line.split(delimiter)[0].strip() for line in lines]  # may contain duplicates.
    star_ids = list(OrderedDict.fromkeys(star_ids_as_found))  # no duplicates, order preserved.
    return star_ids