import re


def extract_place_statvar(series_id, counters):
    """Given the series_id, extract the raw place and stat-var ID.

    Args:
        series_id: EIA series ID
        counters: map for updating error statistics

    Returns a (place, raw-stat-var, is_us_place) tuple.
    """

    if series_id.startswith('ELEC.PLANT.'):
        counters['error_unimplemented_plant_series'] += 1
        return (None, None, None)

    # ELEC.{MEASURE}.{FUEL_TYPE}-{PLACE}-{PRODUCER_SECTOR}.{PERIOD}
    m = re.match(r"^ELEC\.([^.]+)\.([^-]+)-([^-]+)-([^.]+)\.([AQM])$",
                 series_id)
    if m:
        measure = m.group(1)
        fuel_type = m.group(2)
        place = m.group(3)
        producing_sector = m.group(4)
        period = m.group(5)
        sv_id = f'ELEC.{measure}.{fuel_type}-{producing_sector}.{period}'
    else:
        # ELEC.{MEASURE}.{PLACE}-{CONSUMER_SECTOR}.{PERIOD}
        m = re.match(r"^ELEC\.([^.]+)\.([^-]+)-([^.]+)\.([AQM])$", series_id)
        if not m:
            counters['error_unparsable_series'] += 1
            return (None, None)

        measure = m.group(1)
        place = m.group(2)
        consuming_sector = m.group(3)
        period = m.group(4)
        sv_id = f'ELEC.{measure}.{consuming_sector}.{period}'

    return (place, sv_id, True)