from repository import SequencerRepository
from typing import Optional


class SequencerService(object):
    def __init__(self, sequencer_repository):
        # type: (SequencerRepository) -> None

        self.sequencer_repository = sequencer_repository

    def update_fiscal_id_sequence(self, initial_order_id, last_order_id):
        # type: (int, int) -> None

        fiscal_id = self.sequencer_repository.get_fiscal_id()
        if fiscal_id == 0:
            fiscal_id = initial_order_id + last_order_id
            self.sequencer_repository.set_fiscal_id(fiscal_id)
        elif initial_order_id > fiscal_id:
            fiscal_id = initial_order_id
            self.sequencer_repository.set_fiscal_id(fiscal_id)

    def build_next_fiscal_id(self):
        # type: () -> int

        fiscal_id = self.sequencer_repository.increment_fiscal_id()
        return fiscal_id
