"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createAdminAppConfig = exports.configureAdminCognitoUserPoolDomain = void 0;
var _createReactAppConfig = require("./createReactAppConfig");
var _tailwindcss = _interopRequireDefault(require("tailwindcss"));
var _traverseLoaders = require("@webiny/project-utils/traverseLoaders");
// @ts-expect-error No types available for this package yet.

const createAdminAppConfig = modifier => {
  return (0, _createReactAppConfig.createReactAppConfig)(baseParams => {
    const {
      config,
      options
    } = baseParams;
    config.customEnv(env => ({
      ...env,
      PORT: process.env.PORT || 3001,
      WEBINY_ADMIN_ENV: options.env,
      WEBINY_ADMIN_TRASH_BIN_RETENTION_PERIOD_DAYS: process.env.WEBINY_TRASH_BIN_RETENTION_PERIOD_DAYS
    }));
    config.pulumiOutputToEnv("apps/api", ({
      output,
      env
    }) => {
      if (!output) {
        return env;
      }
      return {
        ...env,
        REACT_APP_USER_POOL_REGION: output.region,
        REACT_APP_GRAPHQL_API_URL: `${output.apiUrl}/graphql`,
        REACT_APP_API_URL: output.apiUrl,
        REACT_APP_USER_POOL_ID: output.cognitoUserPoolId,
        REACT_APP_USER_POOL_WEB_CLIENT_ID: output.cognitoAppClientId,
        REACT_APP_USER_POOL_PASSWORD_POLICY: JSON.stringify(output.cognitoUserPoolPasswordPolicy),
        REACT_APP_WEBSOCKET_URL: output.websocketApiUrl
      };
    });

    /**
     * Add Tailwind loader to webpack configuration.
     */
    config.webpack(config => {
      (0, _traverseLoaders.traverseLoaders)(config.module?.rules, loader => {
        if (loader.loader && loader.loader.includes("postcss-loader")) {
          loader.options.postcssOptions.plugins = [...loader.options.postcssOptions.plugins(), (0, _tailwindcss.default)({
            config: require.resolve("@webiny/admin-ui/tailwind.config")
          })];
        }
      });
      return config;
    });
    config.rspack(config => {
      (0, _traverseLoaders.traverseLoaders)(config.module?.rules, loader => {
        if (loader.loader && loader.loader.includes("postcss-loader")) {
          loader.options.postcssOptions.plugins = [...loader.options.postcssOptions.plugins, (0, _tailwindcss.default)({
            config: require.resolve("@webiny/admin-ui/tailwind.config")
          })];
        }
      });
      return config;
    });
    if (modifier) {
      modifier(baseParams);
    }
  });
};

/**
 * Inject REACT_APP_USER_POOL_DOMAIN from the `core` app output into the `admin` app bundle.
 * The Cognito user pool domain is taken from the `cognitoUserPoolDomain` Pulumi app output.
 */
exports.createAdminAppConfig = createAdminAppConfig;
const configureAdminCognitoUserPoolDomain = modifier => {
  modifier.config.pulumiOutputToEnv("apps/core", ({
    env,
    output
  }) => {
    return {
      ...env,
      REACT_APP_USER_POOL_DOMAIN: output["cognitoUserPoolDomain"]
    };
  });
};
exports.configureAdminCognitoUserPoolDomain = configureAdminCognitoUserPoolDomain;

//# sourceMappingURL=createAdminAppConfig.js.map