"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.kubeClusterHostScheme = exports.endpointBaseUrl = exports.appExternalUrls = void 0;
/**
 * Return proper SDM goal externalUrls structure or `undefined` if
 * there is no externally accessible endpoint.
 */
function appExternalUrls(ka) {
    const url = endpointBaseUrl(ka);
    if (!url) {
        return undefined;
    }
    const label = url.split(":")[0];
    return [{ label, url }];
}
exports.appExternalUrls = appExternalUrls;
/**
 * Create the URL for a deployment using the protocol, host, and path
 * from the [[KubernetesApplication]] object.  If `ka.path` is not
 * truthy, no ingress was created so return `undefined`.  If the path
 * does not begin and end with a forward slash, /, add them.  If the
 * ingress spec has a TLS secret the scheme is set to "https",
 * otherwise it is set to "http".  If there is not enough information
 * in the ingress spec to create an endpoint, `undefined` is returned.
 *
 * @param ka Kubernetes application
 * @return endpoint URL for deployment service
 */
function endpointBaseUrl(ka) {
    const path = endpointPath(ka);
    if (!path) {
        return undefined;
    }
    const schemeHost = kubeClusterHostScheme(ka);
    if (!schemeHost) {
        return undefined;
    }
    return `${schemeHost}${path}`;
}
exports.endpointBaseUrl = endpointBaseUrl;
/**
 * Determine path for endpoint URL.  If `ka.path` is truthy, return it
 * after ensuring it starts and ends with single forward slash, "/".
 * (Typically an ingress path begins with a forward slash but does not
 * end with one.)  Otherwise, return `undefined`.
 *
 * @param ka Kubernetes application information
 * @return Standardized URL path that begins and ends with a single forward slash or `undefined`
 */
function endpointPath(ka) {
    if (!ka.path) {
        return undefined;
    }
    const path = (ka.path.startsWith("/")) ? ka.path : "/" + ka.path;
    const tail = (path.endsWith("/")) ? path : path + "/";
    return tail;
}
/**
 * Determine host and scheme for endpoint.  The host will be the
 * `host` property of the first element of `ka.ingressSpec.spec.rules`
 * that defines a host.  The scheme will be "https" if the host
 * appears in the list of hostnames for any ingress TLS secret,
 * otherwise "http".  If there is no ingress spec or no ingress spec
 * rules contain a host, return `undefined`.
 *
 * @param ka Kubernetes application information
 * @return "scheme://hostname" as determined from the ingress spec, or `undefined`
 */
function kubeClusterHostScheme(ka) {
    var _a, _b, _c, _d, _e, _f;
    const hostRule = (_c = (_b = (_a = ka === null || ka === void 0 ? void 0 : ka.ingressSpec) === null || _a === void 0 ? void 0 : _a.spec) === null || _b === void 0 ? void 0 : _b.rules) === null || _c === void 0 ? void 0 : _c.find(r => !!r.host);
    if (!hostRule) {
        return undefined;
    }
    const host = hostRule.host;
    const tlsSecret = (_f = (_e = (_d = ka.ingressSpec) === null || _d === void 0 ? void 0 : _d.spec) === null || _e === void 0 ? void 0 : _e.tls) === null || _f === void 0 ? void 0 : _f.find(t => { var _a; return (_a = t.hosts) === null || _a === void 0 ? void 0 : _a.some(h => h === host); });
    const scheme = (tlsSecret) ? "https" : "http";
    return `${scheme}://${host}`;
}
exports.kubeClusterHostScheme = kubeClusterHostScheme;
//# sourceMappingURL=externalUrls.js.map