{"size": 2094, "ext": "js", "max_stars_repo_path": "CSSObject/rules/MediaRule.js", "max_stars_repo_name": "alt-art/CSSObject.js", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "CSSObject/rules/MediaRule.js", "max_issues_repo_name": "alt-art/CSSObject.js", "max_issues_repo_issues_event_min_datetime": "2022-01-31T04:30:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-01-31T04:30:27.000Z", "max_forks_repo_path": "CSSObject/rules/MediaRule.js", "max_forks_repo_name": "alt-art/CSSObject.js", "max_forks_repo_forks_event_min_datetime": "2021-09-17T22:19:09.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-04T22:42:12.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 2, "avg_line_length": 28.6849315068, "max_line_length": 81, "alphanum_fraction": 0.4498567335}
import Rule from "./Rule.js"
import ICSS from "../enums/ICSS.js"
import IMedia from "../enums/IMedia.js"
import ParserBlock from "../parser/ParserBlock.js"
import BlocksParser from "../parser/BlocksParser.js"
import QuerieRule from "./QuerieRule.js"


/**
 * Class MediaRule
 * @class MediaRule
 */
class MediaRule extends BlocksParser(ParserBlock) {

    /**
     * Initialize MediaRule
     * @param {string} query
     * @param {string} blocks
     * @returns {MediaRule}
     */
    constructor(query, blocks) {
        super()

        this.media = { query, blocks }
        return this
    }

    set media({query, blocks}) {
        this.querie = query
        this.blocks = blocks
            .split(ICSS.BLOCK.END)
            .map(b => this.block(b))
    }

    set querie(value) {
        this.query = value
        this.queries = this.query.split(ICSS.COMMA)
            .map(qrie => {
                let mediaQuerie = {rules: [], types: [], only: false, not: false}

                qrie.split(/and+(\s+\([^)]*\))?/g)
                    .map(i => i.trim()).filter(i => i != '')
                    .map(r => {
                        let rule = r.trim()

                        if (rule.includes(ICSS.DOTS)) {
                            let [ prop, value ] = rule.replace(/[\(\)]/g, '')
                                .split(ICSS.DOTS).map(i => i.trim())
                            
                            mediaQuerie.rules.push(new Rule(prop, value))
                            return
                        }

                        IMedia.ONLY_OR_NOT.map(operator => {
                            if (rule.startsWith(operator)) {
                                rule = rule.replace(operator, '').trim()
                                mediaQuerie[operator] = true
                            }
                        })

                        if (IMedia.MEDIA_TYPES.includes(rule))
                            mediaQuerie.types.push(rule)
                })

                return new QuerieRule(mediaQuerie)
            })
    }

}


export default MediaRule
