import { r as registerInstance, h, H as Host } from './index-ecdfb761.js';
import { d as dark } from './dark-51789ed8.js';
import { D as DataApiImpl } from './dataApi-f887ca8c.js';
import { K as KimlikApiImpl } from './kimlik-api-fddeac81.js';
import { i as isUserLoggedIn } from './index-199df3c1.js';
import { C as CookieStorage } from './cookie-5d9f5819.js';
import { A as APIBase } from './apiBase-6015f41f.js';
import { p as prod } from './services-63217112.js';
import { l as light } from './light-e0bd183b.js';
import './colors-4dc9e03b.js';
import './data-transfer-field-2f305919.js';
import './BaseField-0b01dc2c.js';
import './filter-cbaa3d7d.js';
import './_commonjsHelpers-27f122dd.js';

class DevingenApi {
  constructor(endpoint, defaultHeaders = {}) {
    // region products
    this.createProduct = (body) => this.api.post(`products`, body);
    this.getProducts = (query = {}) => this.api.get('products', query);
    this.getProduct = (id) => this.api.get(`products/${id}`);
    this.updateProduct = (id, body) => this.api.put(`products/${id}`, body);
    this.deleteProduct = (id) => this.api.delete(`products/${id}`);
    // endregion
    // region workspace
    this.createWorkspace = (name) => this.api.post(`workspaces`, { name });
    this.updateWorkspace = (id, body) => this.api.put(`workspaces/${id}`, body);
    this.deleteWorkspace = (id) => this.api.delete(`workspaces/${id}`);
    // endregion
    this.getWorkspaceOwnerships = () => this.api.get('workspace-ownerships');
    // region product-settings-kimlik
    this.createProductSettingsKimlik = (productId, databaseProductId, webhookAddress, headers) => this.api.post(`product-settings-kimlik`, { product: { _id: productId }, databaseProduct: { _id: databaseProductId }, webhookAddress, headers });
    this.findProductSettingsKimlik = (productId) => this.api.get(`product-settings-kimlik`, { productId });
    // endregion
    // region product-settings-taban
    this.createProductSettingsTaban = (productId) => this.api.post(`product-settings-taban`, { product: { _id: productId } });
    this.findProductSettingsTaban = (productId) => this.api.get(`product-settings-taban`, { productId });
    this.api = new APIBase(endpoint, defaultHeaders);
  }
}

/**
 * Returns the current appearance, starts listening to the appearance change events and notifies the callback.
 *
 * @param callback
 */
function watchDeviceAppearance(callback) {
  window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (event) => {
    callback(event.matches ? 'dark' : 'light');
  });
  if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
    return 'dark';
  }
  return 'light';
}

const wrapContent = (theme, content) => (h(Host, null, h("dvn-theme-provider", { theme: theme, colorBackground: true, fitHeight: true }, h("dvn-global-table", null), h("dvn-global-grid", null), h("dvn-global-text", null), h("notifier-wrapper", null, content))));
const DevingenRoot = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.theme = light;
  }
  componentWillLoad() {
    // get the current appearance from device and start watching the appearance change events
    if (watchDeviceAppearance(appearance => this.theme = appearance === 'dark' ? dark : light) === 'dark') {
      this.theme = dark;
    }
  }
  render() {
    const { theme } = this;
    const jwt = new CookieStorage().getAll().jwt;
    const headers = {
      Authorization: jwt ? `Bearer ${new CookieStorage().getAll().jwt}` : undefined,
    };
    const apiKimlik = new KimlikApiImpl(`${prod.kimlik}/${prod.base}`, headers);
    const apiData = new DataApiImpl(`${prod.data}/${prod.base}`, headers);
    const apiDevingen = new DevingenApi(prod.devingen, headers);
    if (!isUserLoggedIn()) {
      return wrapContent(theme, h("devingen-login-page", { apiKimlik: apiKimlik }));
    }
    return wrapContent(theme, h("devingen-router", { apiKimlik: apiKimlik, apiDevingen: apiDevingen, apiData: apiData }));
  }
};

export { DevingenRoot as devingen_root };
