/*
 * Licensed to Elasticsearch B.V. under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch B.V. licenses this file to you 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.
 */
import { parse } from 'url';
/**
 * If the url is determined to contain an appId and appPath, it returns those portions. If it is not in the right
 * format and an appId and appPath can't be extracted, it returns an empty object.
 * @param {string} url - a relative or absolute url which contains an appPath, an appId, and optionally, a basePath.
 * @param {string} basePath - optional base path, if given should start with "/".
 */
export function extractAppPathAndId(url, basePath) {
    if (basePath === void 0) { basePath = ''; }
    var parsedUrl = parse(url);
    if (!parsedUrl.path) {
        return {};
    }
    var pathWithoutBase = parsedUrl.path.slice(basePath.length);
    if (!pathWithoutBase.startsWith('/app/')) {
        return {};
    }
    var appPath = parsedUrl.hash && parsedUrl.hash.length > 0 ? parsedUrl.hash.slice(1) : '';
    return { appId: pathWithoutBase.slice('/app/'.length), appPath: appPath };
}
