"use strict";

var Fashion = require('../../export/Base.js');
var BaseSelector = require('./BaseSelector.js');

class SelectorPart extends BaseSelector {
    constructor(value, type) {
        super();
        this.value = value;
        this.selectorType = type;
    }

    doVisit(visitor) {
        visitor.selector(this);
    }

    descend(visitor) {
        this.value && visitor.visit(this.value);
    }

    toString() {
        switch (this.selectorType) {
            case 'placeholder':
                return '%' + this.value.toString();
            case 'dash':
                return '-' + this.value.toString();
            case 'attribute':
                return '[' + this.value.toString() + ']';
            case 'pseudo':
                return ':' + this.value.toString();
            default:
                return this.value.toString();
        }
    }

    clone(match, replace) {
        if (match && match === this.toString()) {
            return replace.clone();
        }
        var cloned = new SelectorPart(this.value, this.selectorType);
        if (this.parent) {
            cloned.setParent(this.parent.clone(match, replace));
        }
        return cloned;
    }

    getTypePriority() {
        switch (this.selectorType) {
            case 'class':
                return 0;
            case 'id':
                return 1;
            case 'pseudo':
                var str = this.value.toString();

                if (str.indexOf(":") === 0) {
                    return 21;
                }

                if (str.indexOf('not') === 0) {
                    return 19;
                }

                return 20;
            case 'attribute':
                return 0;
            case 'placeholder':
                return -100;
            default:
                return -50;
        }
    }
}

Fashion.apply(SelectorPart.prototype, {
    type: 'selector',
    $isFashionSelectorPart: true,
    value: null,
    selectorType: null
});

module.exports = SelectorPart;