<commit_before>// Copyright (c) 2010-2019 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0

import Foundation
import os.log

enum IconType: Int {
    case png
    case svg
}

struct Endpoint {
    let baseURL: String
    let path: String
    var queryItems: [URLQueryItem]
}

extension Endpoint {
    var url: URL? {
        var components = URLComponents(string: baseURL)
        components?.path = path
        components?.queryItems = queryItems
        os_log("URL: %{PUBLIC}@", log: OSLog.urlComposition, type: .debug, components?.url?.absoluteString ?? "")
        return components?.url
    }

    static func watchSitemap(openHABRootUrl: String, sitemapName: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/sitemaps/" + sitemapName,
                        queryItems: [URLQueryItem(name: "jsoncallback", value: "callback")])
    }

    static func appleRegistration(prefsURL: String,
                                  deviceToken: String,
                                  deviceId: String,
                                  deviceName: String) -> Endpoint {
        return Endpoint(baseURL: prefsURL,
                        path: "/addAppleRegistration",
                        queryItems: [URLQueryItem(name: "regId", value: deviceToken),
                                     URLQueryItem(name: "deviceId", value: deviceId),
                                     URLQueryItem(name: "deviceModel", value: deviceName)])
    }

    static func notification(prefsURL: String) -> Endpoint {
        return Endpoint(baseURL: prefsURL,
                        path: "/api/v1/notifications",
                        queryItems: [URLQueryItem(name: "limit", value: "20")])
    }

    static func tracker(openHABRootUrl: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/bindings",
                        queryItems: [])
    }

    static func sitemaps(openHABRootUrl: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/sitemaps",
                        queryItems: [URLQueryItem(name: "limit", value: "20")])
    }

    // swiftlint:disable:next function_parameter_count
    static func chart(rootUrl: String, period: String?, type: String?, service: String?, name: String?, legend: Bool?, theme: ChartStyle = .light) -> Endpoint {
        let random = Int.random(in: 0 ..< 1000)
        var endpoint = Endpoint(baseURL: rootUrl,
                                path: "/chart",
                                queryItems: [URLQueryItem(name: "period", value: period),
                                             URLQueryItem(name: "random", value: String(random))])

        if let type = type, type.isAny(of: "GroupItem", "Group") {
            endpoint.queryItems.append(URLQueryItem(name: "groups", value: name))
        } else {
            endpoint.queryItems.append(URLQueryItem(name: "items", value: name))
        }
        if let service = service, !service.isEmpty {
            endpoint.queryItems.append(URLQueryItem(name: "service", value: service))
        }
        if let legend = legend, legend != false {<commit_msg>fix chart legend, closes #481 (#482)<commit_after>// Copyright (c) 2010-2019 Contributors to the openHAB project
//
// See the NOTICE file(s) distributed with this work for additional
// information.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0
//
// SPDX-License-Identifier: EPL-2.0

import Foundation
import os.log

enum IconType: Int {
    case png
    case svg
}

struct Endpoint {
    let baseURL: String
    let path: String
    var queryItems: [URLQueryItem]
}

extension Endpoint {
    var url: URL? {
        var components = URLComponents(string: baseURL)
        components?.path = path
        components?.queryItems = queryItems
        os_log("URL: %{PUBLIC}@", log: OSLog.urlComposition, type: .debug, components?.url?.absoluteString ?? "")
        return components?.url
    }

    static func watchSitemap(openHABRootUrl: String, sitemapName: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/sitemaps/" + sitemapName,
                        queryItems: [URLQueryItem(name: "jsoncallback", value: "callback")])
    }

    static func appleRegistration(prefsURL: String,
                                  deviceToken: String,
                                  deviceId: String,
                                  deviceName: String) -> Endpoint {
        return Endpoint(baseURL: prefsURL,
                        path: "/addAppleRegistration",
                        queryItems: [URLQueryItem(name: "regId", value: deviceToken),
                                     URLQueryItem(name: "deviceId", value: deviceId),
                                     URLQueryItem(name: "deviceModel", value: deviceName)])
    }

    static func notification(prefsURL: String) -> Endpoint {
        return Endpoint(baseURL: prefsURL,
                        path: "/api/v1/notifications",
                        queryItems: [URLQueryItem(name: "limit", value: "20")])
    }

    static func tracker(openHABRootUrl: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/bindings",
                        queryItems: [])
    }

    static func sitemaps(openHABRootUrl: String) -> Endpoint {
        return Endpoint(baseURL: openHABRootUrl,
                        path: "/rest/sitemaps",
                        queryItems: [URLQueryItem(name: "limit", value: "20")])
    }

    // swiftlint:disable:next function_parameter_count
    static func chart(rootUrl: String, period: String?, type: String?, service: String?, name: String?, legend: Bool?, theme: ChartStyle = .light) -> Endpoint {
        let random = Int.random(in: 0 ..< 1000)
        var endpoint = Endpoint(baseURL: rootUrl,
                                path: "/chart",
                                queryItems: [URLQueryItem(name: "period", value: period),
                                             URLQueryItem(name: "random", value: String(random))])

        if let type = type, type.isAny(of: "GroupItem", "Group") {
            endpoint.queryItems.append(URLQueryItem(name: "groups", value: name))
        } else {
            endpoint.queryItems.append(URLQueryItem(name: "items", value: name))
        }
        if let service = service, !service.isEmpty {
            endpoint.queryItems.append(URLQueryItem(name: "service", value: service))
        }
        if let legend = legend {
{'fraction_non_alphanumeric': 0.1089615931721195, 'fraction_numerical': 0.007681365576102418, 'mean_word_length': 2.0008536064874094, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1035287', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1830, 'n_words': 499}