/*
 * Copyright (C) 2016 Reece H. Dunn
 *
 * 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.
 */
package uk.co.reecedunn.intellij.plugin.xquery.psi.impl.xquery;

import com.intellij.extapi.psi.ASTWrapperPsiElement;
import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryModuleImport;
import uk.co.reecedunn.intellij.plugin.xquery.ast.xquery.XQueryNCName;
import uk.co.reecedunn.intellij.plugin.xquery.parser.XQueryElementType;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespace;
import uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryNamespaceProvider;

public class XQueryModuleImportPsiImpl extends ASTWrapperPsiElement implements XQueryModuleImport, XQueryNamespaceProvider {
    public XQueryModuleImportPsiImpl(@NotNull ASTNode node) {
        super(node);
    }

    @Override
    public XQueryNamespace resolveNamespace(CharSequence prefix) {
        XQueryNCName name = findChildByType(XQueryElementType.NCNAME);
        if (name == null) {
            return null;
        }

        if (name.getLocalName().getText().equals(prefix)) {
            PsiElement element = findChildByType(XQueryElementType.URI_LITERAL);
            return new XQueryNamespace(name.getLocalName(), element, this);
        }
        return null;
    }
}

@@ -820,16 +820,59 @@
 otNull;%0A
+import org.jetbrains.annotations.Nullable;%0A
 import u
@@ -920,34 +920,17 @@
 .xquery.
-XQueryModuleImport
+*
 ;%0Aimport
@@ -973,30 +973,31 @@
 ery.
-ast.xqu
+pars
 er
-y
 .XQuery
-NCNam
+ElementTyp
 e;%0Ai
@@ -1042,39 +1042,104 @@
 xquery.p
-arser.XQueryElementType
+si.PsiNavigation;%0Aimport uk.co.reecedunn.intellij.plugin.xquery.psi.XQueryModuleProvider
 ;%0Aimport
@@ -1397,16 +1397,38 @@
 Provider
+, XQueryModuleProvider
  %7B%0A    p
@@ -1981,11 +1981,665 @@
 l;%0A    %7D
+%0A%0A    @SuppressWarnings(%22ConstantConditions%22)%0A    @Nullable%0A    @Override%0A    public XQueryProlog getReferencedProlog() %7B%0A        PsiElement uri = getFirstChild();%0A        while (uri != null) %7B%0A            if (uri instanceof XQueryUriLiteral) %7B%0A                PsiElement file = uri.getReference().resolve();%0A                if (file instanceof XQueryFile) %7B%0A                    PsiElement module = PsiNavigation.findChildByClass(file, XQueryModule.class);%0A                    return PsiNavigation.findChildByClass(module, XQueryProlog.class);%0A                %7D%0A            %7D%0A%0A            uri = uri.getNextSibling();%0A        %7D%0A%0A        return null;%0A    %7D
 %0A%7D%0A

Implement XQueryModuleProvider on ModuleImport.