// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import type * as Common from '../../../core/common/common.js';
import * as Root from '../../../core/root/root.js';
import * as ThemeSupport from '../../legacy/theme_support/theme_support.js';

import {CSS_RESOURCES_TO_LOAD_INTO_RUNTIME} from './get-stylesheet.js';

/**
 * Houses any setup required to run the component docs server. Currently this is
 * only populating the runtime CSS cache but may be extended in the future.
 */
export async function setup(): Promise<void> {
  const setting = {
    get() {
      return 'default';
    },
  } as Common.Settings.Setting<string>;
  ThemeSupport.ThemeSupport.instance({forceNew: true, setting});

  const allPromises = CSS_RESOURCES_TO_LOAD_INTO_RUNTIME.map(resourcePath => {
    return fetch('/front_end/' + resourcePath).then(response => response.text()).then(cssText => {
      Root.Runtime.cachedResources.set(resourcePath, cssText);
    });
  });

  await Promise.all(allPromises);
}

// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import type * as Common from '../../../core/common/common.js';
import * as Root from '../../../core/root/root.js';
import * as ThemeSupport from '../../legacy/theme_support/theme_support.js';

import {CSS_RESOURCES_TO_LOAD_INTO_RUNTIME} from './get-stylesheet.js';

/**
 * Houses any setup required to run the component docs server. Currently this is
 * only populating the runtime CSS cache but may be extended in the future.
 */
export async function setup(resourcesPrefix = ''): Promise<void> {
  const setting = {
    get() {
      return 'default';
    },
  } as Common.Settings.Setting<string>;
  ThemeSupport.ThemeSupport.instance({forceNew: true, setting});

  const allPromises = CSS_RESOURCES_TO_LOAD_INTO_RUNTIME.map(resourcePath => {
    return fetch(resourcesPrefix + '/front_end/' + resourcePath).then(response => response.text()).then(cssText => {
      Root.Runtime.cachedResources.set(resourcePath, cssText);
    });
  });

  await Promise.all(allPromises);
}

Allow component server setup to take a url prefix for resources
Allow component server setup to take a url prefix for resources

Bug: none
Change-Id: I206766a1ff9bb69c3802671aceb8e5c28545c4cf
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3109530
Commit-Queue: Jack Franklin <993fcadce4d04090da2fefd557a0995e7966c8d5@chromium.org>
Commit-Queue: Tim van der Lippe <dba8716ee7f8d16236046f74d2167cb94410f6ed@chromium.org>
Auto-Submit: Jack Franklin <993fcadce4d04090da2fefd557a0995e7966c8d5@chromium.org>
Reviewed-by: Tim van der Lippe <dba8716ee7f8d16236046f74d2167cb94410f6ed@chromium.org>
