<?xml version="1.0" encoding="utf-8"?>

<div
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:py="http://www.whads.com/ns/cocktail/templates">

    <?py-class
    items = None
    grouping = None
    search_threshold = 10
    searchable_options = {"entryGroupsSelector": ".group"}

    def should_enable_search(self):
        if not self.items:
            return False

        if not self.search_threshold:
            return True

        if self.grouped:
            item_count = sum(
                len(group_items)
                for group, group_items in self.items
            )
        else:
            item_count = len(self.items)

        return item_count >= self.search_threshold
    ?>

    <?py
    self.add_resource("/cocktail/scripts/xregexp.js")
    self.add_resource("/cocktail/scripts/searchable.js")
    self.add_resource("/cocktail/scripts/SearchableCheckList.js")
    self.searchable_options = self.searchable_options.copy()
    ?>

    <py:ready>
        <?py
        if self.should_enable_search:
            self.add_class("search_enabled")
            self.set_client_param("searchableOptions", self.searchable_options)
        ?>
    </py:ready>

    <div
        py:id="search_controls"
        py:client_model="cocktail.html.SearchableCheckList.searchControls">
        <input py:id="search_box" type="search"/>
        <div py:id="toggle_selection_links">
            <a py:local_id="select_all_link" href="javascript:">
                ${translations("cocktail.html.SearchableCheckList.search_controls.select_all_link")}
            </a>
            <a py:local_id="empty_selection_link" href="javascript:">
                ${translations("cocktail.html.SearchableCheckList.search_controls.empty_selection_link")}
            </a>
        </div>
    </div>

    <py:cocktail.html.CheckList
        py:id="check_list"
        py:items="@{self.items}"
        py:grouping="@{self.grouping}"/>

    <?py
    self.data_binding_delegate = self.check_list
    ?>

</div>

