var _a, _b, _c, _d;
import Adw from 'gi://Adw';
import Gdk from 'gi://Gdk';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import GObject from 'gi://GObject';
import Gtk from 'gi://Gtk';
import * as Settings from './../settings.js';
import { gettext as _ } from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
/**
 * Subclass containing the preferences for Wallhaven adapter
 */
class WallhavenSettings extends Adw.PreferencesPage {
    /**
     * Craft a new adapter using an unique ID.
     *
     * Previously saved settings will be used if the adapter and ID match.
     *
     * @param {string} id Unique ID
     */
    constructor(id) {
        super(undefined);
        const path = `${Settings.RWG_SETTINGS_SCHEMA_PATH}/sources/wallhaven/${id}/`;
        this._settings = new Settings.Settings(Settings.RWG_SETTINGS_SCHEMA_SOURCES_WALLHAVEN, path);
        this._settings.bind('ai-art', this._ai_art, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('allow-nsfw', this._allow_nsfw, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('allow-sfw', this._allow_sfw, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('allow-sketchy', this._allow_sketchy, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('api-key', this._api_key, 'text', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('category-anime', this._category_anime, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('category-general', this._category_general, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('category-people', this._category_people, 'active', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('color', this._row_color, 'subtitle', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('keyword', this._keyword, 'text', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('minimal-resolution', this._minimal_resolution, 'text', Gio.SettingsBindFlags.DEFAULT);
        this._settings.bind('aspect-ratios', this._aspect_ratios, 'text', Gio.SettingsBindFlags.DEFAULT);
        this._button_color_undo.connect('clicked', () => {
            this._row_color.subtitle = '';
        });
        this._button_color.connect('clicked', () => {
            // TODO: For GTK 4.10+
            // Gtk.ColorDialog();
            // https://stackoverflow.com/a/54487948
            this._colorDialog = new Gtk.ColorChooserDialog({
                title: _('Choose a Color'),
                transient_for: this.get_root() ?? undefined,
                modal: true,
            });
            this._colorDialog.set_use_alpha(false);
            if (!_a._colorPalette) {
                _a._colorPalette = [];
                _a._availableColors.forEach(hexColor => {
                    const rgbaColor = new Gdk.RGBA();
                    rgbaColor.parse(hexColor);
                    _a._colorPalette.push(rgbaColor);
                });
            }
            this._colorDialog.add_palette(Gtk.Orientation.HORIZONTAL, 10, _a._colorPalette);
            this._colorDialog.connect('response', (dialog, response_id) => {
                if (response_id === Gtk.ResponseType.OK) {
                    // result is a Gdk.RGBA which uses float
                    const rgba = dialog.get_rgba();
                    // convert to rgba so it's useful
                    const rgbaString = rgba.to_string() ?? 'rgb(0,0,0)';
                    const rgbaArray = rgbaString.replace('rgb(', '').replace(')', '').split(',');
                    const hexString = `${parseInt(rgbaArray[0]).toString(16).padStart(2, '0')}${parseInt(rgbaArray[1]).toString(16).padStart(2, '0')}${parseInt(rgbaArray[2]).toString(16).padStart(2, '0')}`;
                    this._row_color.subtitle = hexString;
                }
                dialog.destroy();
            });
            this._colorDialog.show();
        });
    }
    /**
     * Clear all config options associated to this specific adapter.
     */
    clearConfig() {
        this._settings.resetSchema();
    }
}
_a = WallhavenSettings, _b = GObject.GTypeName, _c = Gtk.template, _d = Gtk.internalChildren;
WallhavenSettings[_b] = 'WallhavenSettings';
// @ts-expect-error Gtk.template is not in the type definitions files yet
WallhavenSettings[_c] = GLib.uri_resolve_relative(import.meta.url, './wallhaven.ui', GLib.UriFlags.NONE);
// @ts-expect-error Gtk.internalChildren is not in the type definitions files yet
WallhavenSettings[_d] = [
    'ai_art',
    'allow_nsfw',
    'allow_sfw',
    'allow_sketchy',
    'api_key',
    'aspect_ratios',
    'button_color_undo',
    'button_color',
    'category_anime',
    'category_general',
    'category_people',
    'keyword',
    'minimal_resolution',
    'row_color',
];
(() => {
    GObject.registerClass(_a);
})();
WallhavenSettings._availableColors = [
    '#660000', '#990000', '#cc0000', '#cc3333', '#ea4c88',
    '#993399', '#663399', '#333399', '#0066cc', '#0099cc',
    '#66cccc', '#77cc33', '#669900', '#336600', '#666600',
    '#999900', '#cccc33', '#ffff00', '#ffcc33', '#ff9900',
    '#ff6600', '#cc6633', '#996633', '#663300', '#000000',
    '#999999', '#cccccc', '#ffffff', '#424153',
];
export { WallhavenSettings };
