# Path: core/model/date.py
# def get_repeat_type_desc(repeat_type, start_date):
#     res = {
#         RepeatType.Daily: tr('Daily'),
#         RepeatType.Weekly: tr('Weekly'),
#         RepeatType.Monthly: tr('Monthly'),
#         RepeatType.Yearly: tr('Yearly'),
#         RepeatType.Weekday: '', # dynamic
#         RepeatType.WeekdayLast: '', # dynamic
#     }[repeat_type]
#     if res:
#         return res
#     date = start_date
#     weekday_name = date.strftime('%A')
#     if repeat_type == RepeatType.Weekday:
#         week_no = (date.day - 1) // 7
#         position = [tr('first'), tr('second'), tr('third'), tr('fourth'), tr('fifth')][week_no]
#         return tr('Every %s %s of the month') % (position, weekday_name)
#     elif repeat_type == RepeatType.WeekdayLast:
#         _, days_in_month = monthrange(date.year, date.month)
#         if days_in_month - date.day < 7:
#             return tr('Every last %s of the month') % weekday_name
#         else:
#             return ''
# 
# Path: core/util.py
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# Path: core/model/date.py
# class RepeatType:
#     # Mirror of RepeatType enum is ccore
#     Daily = 1
#     Weekly = 2
#     Monthly = 3
#     Yearly = 4
#     Weekday = 5
#     WeekdayLast = 6
#     ALL = [Daily, Weekly, Monthly, Yearly, Weekday, WeekdayLast]
#     # used for serialization
#     AS_STR = ['daily', 'weekly', 'monthly', 'yearly', 'weekday', 'weekday_last']
# 
#     @classmethod
#     def to_str(cls, i):
#         try:
#             return cls.AS_STR[i-1]
#         except IndexError:
#             return 'monthly'
# 
#     @classmethod
#     def from_str(cls, s):
#         try:
#             return cls.AS_STR.index(s) + 1
#         except ValueError:
#             return cls.Monthly
# 
# Path: core/gui/transaction_panel.py
# class PanelWithTransaction(GUIPanel):
#     """Base class for panels working with a transaction"""
#     def __init__(self, mainwindow):
#         GUIPanel.__init__(self, mainwindow)
#         self.transaction = Transaction(date.today())
#         self._selected_splits = []
#         # Place to store temporarily created accounts during the editing of the
#         # transaction.
#         self._tmpaccounts = AccountList(self.document.default_currency)
#         # completable_edit has to be set before split_table is created because split table fetches
#         # our completable edit on __init__ (for Qt).
#         self.completable_edit = CompletableEdit(mainwindow)
#         self.split_table = SplitTable(weakref.proxy(self))
# 
#     def change_split(self, split, account_name, amount, memo):
#         if split is None:
#             split = self.transaction.new_split()
#         if account_name:
#             if split.account:
#                 account_type = split.account.type
#             else:
#                 account_type = AccountType.Expense if split.amount < 0 else AccountType.Income
#             split.account = self._tmpaccounts.find(account_name, account_type)
#         else:
#             split.account = None
#         split.amount = amount
#         split.memo = memo
#         self.transaction.balance(split)
#         self.split_table.refresh_splits()
#         self.view.refresh_for_multi_currency()
# 
#     def delete_split(self, split):
#         self.transaction.remove_split(split)
#         self.view.refresh_for_multi_currency()
# 
#     def select_splits(self, splits):
#         self._selected_splits = splits
# 
#     # --- Properties
#     @property
#     def description(self):
#         return self.transaction.description
# 
#     @description.setter
#     def description(self, value):
#         self.transaction.description = value
# 
#     @property
#     def payee(self):
#         return self.transaction.payee
# 
#     @payee.setter
#     def payee(self, value):
#         self.transaction.payee = value
# 
#     @property
#     def checkno(self):
#         return self.transaction.checkno
# 
#     @checkno.setter
#     def checkno(self, value):
#         self.transaction.checkno = value
# 
#     @property
#     def notes(self):
#         return self.transaction.notes
# 
#     @notes.setter
#     def notes(self, value):
#         self.transaction.notes = value
# 
#     @property
#     def is_multi_currency(self):
#         return self.transaction.is_mct


from datetime import date
from core.model.date import get_repeat_type_desc
from core.util import first
from core.trans import tr
from ..exception import OperationAborted
from ..model._ccore import Recurrence, Transaction
from ..model.date import RepeatType
from .selectable_list import LinkedSelectableList
from .transaction_panel import PanelWithTransaction

    @repeat_every.setter
    def repeat_every(self, value):
        value = max(1, value)
        self.schedule.change(repeat_every=value)
        self.view.refresh_repeat_every()

    @property
    def repeat_every_desc(self):
        if self.schedule.repeat_every > 1:
            return REPEAT_EVERY_DESCS_PLURAL[self.schedule.repeat_type]
        else:
            return REPEAT_EVERY_DESCS[self.schedule.repeat_type]

    @property
    def repeat_type(self):
        return self.schedule.repeat_type

    @repeat_type.setter
    def repeat_type(self, value):
        if value == self.schedule.repeat_type:
            return
        self.schedule.change(repeat_type=value)
        self.view.refresh_repeat_every()

    def create_repeat_type_list(self):
        self.repeat_type_list = LinkedSelectableList(setfunc=self._update_repeat_type_selection)

class SchedulePanel(PanelWithTransaction, WithScheduleMixIn):
    def __init__(self, mainwindow):

# Path: core/model/date.py
# def get_repeat_type_desc(repeat_type, start_date):
#     res = {
#         RepeatType.Daily: tr('Daily'),
#         RepeatType.Weekly: tr('Weekly'),
#         RepeatType.Monthly: tr('Monthly'),
#         RepeatType.Yearly: tr('Yearly'),
#         RepeatType.Weekday: '', # dynamic
#         RepeatType.WeekdayLast: '', # dynamic
#     }[repeat_type]
#     if res:
#         return res
#     date = start_date
#     weekday_name = date.strftime('%A')
#     if repeat_type == RepeatType.Weekday:
#         week_no = (date.day - 1) // 7
#         position = [tr('first'), tr('second'), tr('third'), tr('fourth'), tr('fifth')][week_no]
#         return tr('Every %s %s of the month') % (position, weekday_name)
#     elif repeat_type == RepeatType.WeekdayLast:
#         _, days_in_month = monthrange(date.year, date.month)
#         if days_in_month - date.day < 7:
#             return tr('Every last %s of the month') % weekday_name
#         else:
#             return ''
# 
# Path: core/util.py
# def first(iterable):
#     """Returns the first item of ``iterable``.
#     """
#     try:
#         return next(iter(iterable))
#     except StopIteration:
#         return None
# 
# Path: core/model/date.py
# class RepeatType:
#     # Mirror of RepeatType enum is ccore
#     Daily = 1
#     Weekly = 2
#     Monthly = 3
#     Yearly = 4
#     Weekday = 5
#     WeekdayLast = 6
#     ALL = [Daily, Weekly, Monthly, Yearly, Weekday, WeekdayLast]
#     # used for serialization
#     AS_STR = ['daily', 'weekly', 'monthly', 'yearly', 'weekday', 'weekday_last']
# 
#     @classmethod
#     def to_str(cls, i):
#         try:
#             return cls.AS_STR[i-1]
#         except IndexError:
#             return 'monthly'
# 
#     @classmethod
#     def from_str(cls, s):
#         try:
#             return cls.AS_STR.index(s) + 1
#         except ValueError:
#             return cls.Monthly
# 
# Path: core/gui/transaction_panel.py
# class PanelWithTransaction(GUIPanel):
#     """Base class for panels working with a transaction"""
#     def __init__(self, mainwindow):
#         GUIPanel.__init__(self, mainwindow)
#         self.transaction = Transaction(date.today())
#         self._selected_splits = []
#         # Place to store temporarily created accounts during the editing of the
#         # transaction.
#         self._tmpaccounts = AccountList(self.document.default_currency)
#         # completable_edit has to be set before split_table is created because split table fetches
#         # our completable edit on __init__ (for Qt).
#         self.completable_edit = CompletableEdit(mainwindow)
#         self.split_table = SplitTable(weakref.proxy(self))
# 
#     def change_split(self, split, account_name, amount, memo):
#         if split is None:
#             split = self.transaction.new_split()
#         if account_name:
#             if split.account:
#                 account_type = split.account.type
#             else:
#                 account_type = AccountType.Expense if split.amount < 0 else AccountType.Income
#             split.account = self._tmpaccounts.find(account_name, account_type)
#         else:
#             split.account = None
#         split.amount = amount
#         split.memo = memo
#         self.transaction.balance(split)
#         self.split_table.refresh_splits()
#         self.view.refresh_for_multi_currency()
# 
#     def delete_split(self, split):
#         self.transaction.remove_split(split)
#         self.view.refresh_for_multi_currency()
# 
#     def select_splits(self, splits):
#         self._selected_splits = splits
# 
#     # --- Properties
#     @property
#     def description(self):
#         return self.transaction.description
# 
#     @description.setter
#     def description(self, value):
#         self.transaction.description = value
# 
#     @property
#     def payee(self):
#         return self.transaction.payee
# 
#     @payee.setter
#     def payee(self, value):
#         self.transaction.payee = value
# 
#     @property
#     def checkno(self):
#         return self.transaction.checkno
# 
#     @checkno.setter
#     def checkno(self, value):
#         self.transaction.checkno = value
# 
#     @property
#     def notes(self):
#         return self.transaction.notes
# 
#     @notes.setter
#     def notes(self, value):
#         self.transaction.notes = value
# 
#     @property
#     def is_multi_currency(self):
#         return self.transaction.is_mct


# Path: core/gui/schedule_panel.py
from datetime import date
from core.model.date import get_repeat_type_desc
from core.util import first
from core.trans import tr
from ..exception import OperationAborted
from ..model._ccore import Recurrence, Transaction
from ..model.date import RepeatType
from .selectable_list import LinkedSelectableList
from .transaction_panel import PanelWithTransaction


    @repeat_every.setter
    def repeat_every(self, value):
        value = max(1, value)
        self.schedule.change(repeat_every=value)
        self.view.refresh_repeat_every()

    @property
    def repeat_every_desc(self):
        if self.schedule.repeat_every > 1:
            return REPEAT_EVERY_DESCS_PLURAL[self.schedule.repeat_type]
        else:
            return REPEAT_EVERY_DESCS[self.schedule.repeat_type]

    @property
    def repeat_type(self):
        return self.schedule.repeat_type

    @repeat_type.setter
    def repeat_type(self, value):
        if value == self.schedule.repeat_type:
            return
        self.schedule.change(repeat_type=value)
        self.view.refresh_repeat_every()

    def create_repeat_type_list(self):
        self.repeat_type_list = LinkedSelectableList(setfunc=self._update_repeat_type_selection)

class SchedulePanel(PanelWithTransaction, WithScheduleMixIn):
    def __init__(self, mainwindow):

        PanelWithTransaction.__init__(self, mainwindow)