'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var alpha = require('@backstage/plugin-scaffolder-common/alpha');
var pluginPermissionNode = require('@backstage/plugin-permission-node');
var router = require('./cjs/router-e667d04e.cjs.js');
var backendPluginApi = require('@backstage/backend-plugin-api');
var backendCommon = require('@backstage/backend-common');
var integration = require('@backstage/integration');
var alpha$2 = require('@backstage/plugin-catalog-node/alpha');
var alpha$1 = require('@backstage/plugin-scaffolder-node/alpha');
require('@backstage/errors');
require('@backstage/catalog-model');
require('@backstage/plugin-scaffolder-node');
require('fs-extra');
require('yaml');
require('zod');
require('path');
require('luxon');
require('globby');
require('isbinaryfile');
require('isolated-vm');
require('lodash/get');
require('@backstage/plugin-scaffolder-backend-module-github');
require('@backstage/plugin-scaffolder-backend-module-gitlab');
require('@backstage/plugin-scaffolder-backend-module-azure');
require('@backstage/plugin-scaffolder-backend-module-bitbucket');
require('@backstage/plugin-scaffolder-backend-module-gerrit');
require('zen-observable');
require('p-queue');
require('@backstage/config');
require('@backstage/plugin-scaffolder-common');
require('express');
require('express-promise-router');
require('jsonschema');
require('uuid');
require('winston');
require('nunjucks');
require('stream');
require('lodash');
require('prom-client');
require('@backstage/plugin-permission-common');
require('url');
require('os');

const templateConditionExports = pluginPermissionNode.createConditionExports({
  pluginId: "scaffolder",
  resourceType: alpha.RESOURCE_TYPE_SCAFFOLDER_TEMPLATE,
  rules: router.scaffolderTemplateRules
});
const actionsConditionExports = pluginPermissionNode.createConditionExports({
  pluginId: "scaffolder",
  resourceType: alpha.RESOURCE_TYPE_SCAFFOLDER_ACTION,
  rules: router.scaffolderActionRules
});
const createScaffolderTemplateConditionalDecision = templateConditionExports.createConditionalDecision;
const scaffolderTemplateConditions = templateConditionExports.conditions;
const createScaffolderActionConditionalDecision = actionsConditionExports.createConditionalDecision;
const scaffolderActionConditions = actionsConditionExports.conditions;

const scaffolderPlugin = backendPluginApi.createBackendPlugin({
  pluginId: "scaffolder",
  register(env) {
    const addedActions = new Array();
    env.registerExtensionPoint(alpha$1.scaffolderActionsExtensionPoint, {
      addActions(...newActions) {
        addedActions.push(...newActions);
      }
    });
    let taskBroker;
    env.registerExtensionPoint(alpha$1.scaffolderTaskBrokerExtensionPoint, {
      setTaskBroker(newTaskBroker) {
        if (taskBroker) {
          throw new Error("Task broker may only be set once");
        }
        taskBroker = newTaskBroker;
      }
    });
    const additionalTemplateFilters = {};
    const additionalTemplateGlobals = {};
    env.registerExtensionPoint(alpha$1.scaffolderTemplatingExtensionPoint, {
      addTemplateFilters(newFilters) {
        Object.assign(additionalTemplateFilters, newFilters);
      },
      addTemplateGlobals(newGlobals) {
        Object.assign(additionalTemplateGlobals, newGlobals);
      }
    });
    env.registerInit({
      deps: {
        logger: backendPluginApi.coreServices.logger,
        config: backendPluginApi.coreServices.rootConfig,
        reader: backendPluginApi.coreServices.urlReader,
        permissions: backendPluginApi.coreServices.permissions,
        database: backendPluginApi.coreServices.database,
        httpRouter: backendPluginApi.coreServices.httpRouter,
        catalogClient: alpha$2.catalogServiceRef
      },
      async init({
        logger,
        config,
        reader,
        database,
        httpRouter,
        catalogClient,
        permissions
      }) {
        const log = backendCommon.loggerToWinstonLogger(logger);
        const actions = [
          ...addedActions,
          ...router.createBuiltinActions({
            integrations: integration.ScmIntegrations.fromConfig(config),
            catalogClient,
            reader,
            config,
            additionalTemplateFilters,
            additionalTemplateGlobals
          })
        ];
        const actionIds = actions.map((action) => action.id).join(", ");
        log.info(
          `Starting scaffolder with the following actions enabled ${actionIds}`
        );
        const router$1 = await router.createRouter({
          logger: log,
          config,
          database,
          catalogClient,
          reader,
          actions,
          taskBroker,
          additionalTemplateFilters,
          additionalTemplateGlobals,
          permissions
        });
        httpRouter.use(router$1);
      }
    });
  }
});

exports.createScaffolderActionConditionalDecision = createScaffolderActionConditionalDecision;
exports.createScaffolderTemplateConditionalDecision = createScaffolderTemplateConditionalDecision;
exports["default"] = scaffolderPlugin;
exports.scaffolderActionConditions = scaffolderActionConditions;
exports.scaffolderTemplateConditions = scaffolderTemplateConditions;
//# sourceMappingURL=alpha.cjs.js.map
