import { NgIf, DatePipe } from '@angular/common';
import * as i0 from '@angular/core';
import { InjectionToken, inject, Component, ChangeDetectionStrategy } from '@angular/core';

const RXAP_ENVIRONMENT = new InjectionToken('rxap-environment');

class EnvironmentComponent {
    constructor() {
        this.environment = inject(RXAP_ENVIRONMENT);
    }
    static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: EnvironmentComponent, deps: [], target: i0.ɵɵFactoryTarget.Component }); }
    static { this.ɵcmp = i0.ɵɵngDeclareComponent({ minVersion: "14.0.0", version: "19.1.3", type: EnvironmentComponent, isStandalone: true, selector: "rxap-environment", host: { classAttribute: "rxap-environment" }, ngImport: i0, template: "<div class=\"p-4\">\n  <h2 class=\"text-xl font-bold pb-2\">{{ environment.app }}</h2>\n  <table class=\"table-auto\">\n    <tr *ngIf=\"environment.name\">\n      <th>Environment</th>\n      <td>{{environment.name}}</td>\n    </tr>\n    <tr>\n      <th>Production</th>\n      <td>{{environment.production}}</td>\n    </tr>\n    <tr *ngIf=\"environment.serviceWorker !== undefined\">\n      <th>Service Worker</th>\n      <td>{{environment.serviceWorker}}</td>\n    </tr>\n    <tr *ngIf=\"environment.release\">\n      <th>Release</th>\n      <td>{{environment.release}}</td>\n    </tr>\n    <tr *ngIf=\"environment.commit\">\n      <th>Commit</th>\n      <td>{{environment.commit}}</td>\n    </tr>\n    <tr *ngIf=\"environment.timestamp\">\n      <th>Timestamp</th>\n      <td>{{environment.timestamp | date:'dd.MM.yy HH:mm:ss'}}</td>\n    </tr>\n    <tr *ngIf=\"environment.branch\">\n      <th>Branch</th>\n      <td>{{environment.branch}}</td>\n    </tr>\n    <tr *ngIf=\"environment.tag\">\n      <th>Tag</th>\n      <td>{{environment.tag}}</td>\n    </tr>\n    <ng-content></ng-content>\n  </table>\n</div>\n", styles: [".collapse-button{background-color:transparent;margin:8px 8px 20px;border:none;cursor:pointer;width:calc(100% - 16px)}table th{text-align:left;padding-right:16px}table th,table td{font-size:75%}\n"], dependencies: [{ kind: "directive", type: NgIf, selector: "[ngIf]", inputs: ["ngIf", "ngIfThen", "ngIfElse"] }, { kind: "pipe", type: DatePipe, name: "date" }], changeDetection: i0.ChangeDetectionStrategy.OnPush }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "19.1.3", ngImport: i0, type: EnvironmentComponent, decorators: [{
            type: Component,
            args: [{ selector: 'rxap-environment', changeDetection: ChangeDetectionStrategy.OnPush, host: { class: 'rxap-environment' }, imports: [NgIf, DatePipe], template: "<div class=\"p-4\">\n  <h2 class=\"text-xl font-bold pb-2\">{{ environment.app }}</h2>\n  <table class=\"table-auto\">\n    <tr *ngIf=\"environment.name\">\n      <th>Environment</th>\n      <td>{{environment.name}}</td>\n    </tr>\n    <tr>\n      <th>Production</th>\n      <td>{{environment.production}}</td>\n    </tr>\n    <tr *ngIf=\"environment.serviceWorker !== undefined\">\n      <th>Service Worker</th>\n      <td>{{environment.serviceWorker}}</td>\n    </tr>\n    <tr *ngIf=\"environment.release\">\n      <th>Release</th>\n      <td>{{environment.release}}</td>\n    </tr>\n    <tr *ngIf=\"environment.commit\">\n      <th>Commit</th>\n      <td>{{environment.commit}}</td>\n    </tr>\n    <tr *ngIf=\"environment.timestamp\">\n      <th>Timestamp</th>\n      <td>{{environment.timestamp | date:'dd.MM.yy HH:mm:ss'}}</td>\n    </tr>\n    <tr *ngIf=\"environment.branch\">\n      <th>Branch</th>\n      <td>{{environment.branch}}</td>\n    </tr>\n    <tr *ngIf=\"environment.tag\">\n      <th>Tag</th>\n      <td>{{environment.tag}}</td>\n    </tr>\n    <ng-content></ng-content>\n  </table>\n</div>\n", styles: [".collapse-button{background-color:transparent;margin:8px 8px 20px;border:none;cursor:pointer;width:calc(100% - 16px)}table th{text-align:left;padding-right:16px}table th,table td{font-size:75%}\n"] }]
        }] });

function DetermineProductionEnvironmentName(environment) {
    if (environment.tag) {
        const tagMatch = environment.tag.match(/^v\d+\.\d+\.\d+(-([^.]+)\.\d+)?$/);
        if (!tagMatch) {
            return undefined;
        }
        // if the tage has a chanel/digest use this as environment name
        return tagMatch[2] ?? 'production';
    }
    else if (environment.branch) {
        return environment.branch;
    }
    return undefined;
}

function DetermineReleaseName(environment, defaultReleaseName = 'local-development') {
    if (environment.tag) {
        return environment.tag;
    }
    else if (environment.branch) {
        return environment.branch;
    }
    return defaultReleaseName;
}

function IsNotReleaseVersion(environment) {
    if (!environment.tag) {
        return true;
    }
    if (environment.tier !== 'production') {
        return true;
    }
    const tagMatch = environment.tag.match(/^v\d+\.\d+\.\d+(-([^.]+)\.\d+)?$/);
    if (!tagMatch) {
        return true;
    }
    if (tagMatch[2]) {
        return true;
    }
    return false;
}

function ProvideEnvironment(environment) {
    return {
        provide: RXAP_ENVIRONMENT,
        useValue: environment,
    };
}

/**
 * Loads the build.json and adds all keys to the environment object
 */
async function UpdateEnvironment(environment, url = 'build.json') {
    await fetch(url)
        .then((response) => {
        if (!response.ok) {
            throw new Error('Could not load build.json');
        }
        return response.json();
    })
        .then((build) => Object.assign(environment, build))
        .catch(error => console.error(error.message));
    console.debug(`environment: '${environment.name}'`, environment);
}

// region environment
// endregion

/**
 * Generated bundle index. Do not edit.
 */

export { DetermineProductionEnvironmentName, DetermineReleaseName, EnvironmentComponent, IsNotReleaseVersion, ProvideEnvironment, RXAP_ENVIRONMENT, UpdateEnvironment };
//# sourceMappingURL=rxap-environment.mjs.map
