class BookmarkableUrlHelper {

    public static String registerClose(String bookmarkableUrl,
                                       final String screenName) {
        final boolean isPerspective = isPerspectiveScreen(bookmarkableUrl,
                                                          screenName);
        final String separator = isPerspective ? PERSPECTIVE_SEP : OTHER_SCREEN_SEP;
        final String closedScreen = CLOSED_PREFIX.concat(screenName);
        final String uniqueScreenAfterDelimiter =
                separator.concat(screenName); // |screen or $screen
        final String firstScreenAfterDelimiter =
                uniqueScreenAfterDelimiter.concat(SEPARATOR); // |screen, or $screen,
        final String commaSeparatedScreen =
                screenName.concat(SEPARATOR); // screen,

        if (isScreenClosed(bookmarkableUrl,
                           closedScreen)) {
            return bookmarkableUrl;
        }
        if (isPerspective) {
            bookmarkableUrl = bookmarkableUrl.replace(screenName,
                                                      closedScreen);
        } else {
            if (bookmarkableUrl.contains(firstScreenAfterDelimiter)) {
                bookmarkableUrl = bookmarkableUrl.replace(firstScreenAfterDelimiter,
                                                          separator);
            } else if (bookmarkableUrl.contains(uniqueScreenAfterDelimiter)) {
                bookmarkableUrl = bookmarkableUrl.replace(uniqueScreenAfterDelimiter,
                                                          "");
            } else if (bookmarkableUrl.contains(commaSeparatedScreen)) {
                bookmarkableUrl = bookmarkableUrl.replace(commaSeparatedScreen,
                                                          "");
            }
        }
        return bookmarkableUrl;
    }

}