// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.2 OR LicenseRef-Slint-commercial

import { ComboBoxBase } from "../common/combobox-base.slint";

export component ComboBox {
    in property <[string]> model <=> i-base.model;
    in property <bool> enabled <=> i-base.enabled;
    out property <bool> has-focus <=> i-base.has-focus;
    in-out property <int> current-index <=> i-base.current-index;
    in-out property <string> current-value <=> i-base.current-value;

    callback selected <=> i-base.selected;

    accessible-role: combobox;
    accessible-value <=> root.current-value;
    forward-focus: i-base;

    HorizontalLayout {
        i-native := NativeComboBox {
            current-value <=> root.current-value;
            has-focus <=> root.has-focus;
            enabled <=> root.enabled;
        }
    }

    i-base := ComboBoxBase {
        width: 100%;
        height: 100%;
        show-popup => {
            i-popup.show();
        }
    }

    i-popup := PopupWindow {
        x: 0;
        y: root.height;
        width: root.width;

        NativeComboBoxPopup {
            width: 100%;
            height: 100%;
        }

        VerticalLayout {
            spacing: 0px;

            for value[index] in root.model: NativeStandardListViewItem {
                item: { text: value };
                is-selected: root.current-index == index;
                has-hover: ta.has-hover;
                combobox: true;

                ta := TouchArea {
                    clicked => {
                        i-base.select(index);
                    }
                }
            }
        }
    }
}

