// 定义 DataGridRow 类型
type DataGridRow = any;

// 定义 DataGrid 类型
type DataGrid = {
    items: any[];
    selectedIndex: number;
    selectedItem: any;
    updateLayout: () => void;
    scrollIntoView: (item: any) => void;
    itemContainerGenerator: {
        containerFromIndex: (index: number) => DataGridRow;
    };
};

// 定义 IsChecked 附加属性相关
class DataGridHelper {
    static getIsChecked(obj: any): boolean | null {
        return obj['IsChecked'] as boolean | null;
    }

    static setIsChecked(obj: any, value: boolean | null): void {
        obj['IsChecked'] = value;
    }

    // 获取指定行
    static getRow(datagrid: DataGrid, rowIndex: number): DataGridRow | null {
        if (!datagrid || datagrid.items.length < 1) return null;
        let row = datagrid.itemContainerGenerator.containerFromIndex(rowIndex);
        if (!row) {
            datagrid.updateLayout();
            datagrid.scrollIntoView(datagrid.items[rowIndex]);
            row = datagrid.itemContainerGenerator.containerFromIndex(rowIndex);
        }
        return row;
    }

    // 获取选择的行
    static getSelectedRow(datagrid: DataGrid): DataGridRow | null {
        if (!datagrid.selectedItem) return null;
        return this.getRow(datagrid, datagrid.selectedIndex);
    }

    // 获取 DataGrid 的所有行
    static* rows(datagrid: DataGrid): Generator<DataGridRow | null> {
        for (let i = 0; i < datagrid.items.length; i++) {
            yield this.getRow(datagrid, i);
        }
    }
}

export default DataGridHelper;